ReAntTweakBar.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. #include "ReAntTweakBar.h"
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <sstream>
  5. #include <iomanip>
  6. #include <map>
  7. #define MAX_CB_VAR_SIZE 10
  8. // Max line size for reading files
  9. #define MAX_LINE 1000
  10. #define MAX_WORD 100
  11. // GLOBAL WRAPPERS
  12. namespace igl
  13. {
  14. std::map<TwType,std::pair<const char *,std::vector<TwEnumVal> > > ReTw_custom_types;
  15. }
  16. TwType igl::ReTwDefineEnum(
  17. const char *name,
  18. const TwEnumVal *enumValues,
  19. unsigned int nbValues)
  20. {
  21. // copy enum valus into vector
  22. std::vector<TwEnumVal> enum_vals;
  23. enum_vals.resize(nbValues);
  24. for(unsigned int j = 0; j<nbValues;j++)
  25. {
  26. enum_vals[j] = enumValues[j];
  27. }
  28. TwType type = TwDefineEnum(name,enumValues,nbValues);
  29. ReTw_custom_types[type] =
  30. std::pair<const char *,std::vector<TwEnumVal> >(name,enum_vals);
  31. return type;
  32. }
  33. namespace igl
  34. {
  35. struct ReTwTypeString
  36. {
  37. TwType type;
  38. const char * type_str;
  39. };
  40. #define RETW_NUM_DEFAULT_TYPE_STRINGS 23
  41. ReTwTypeString ReTwDefaultTypeStrings[RETW_NUM_DEFAULT_TYPE_STRINGS] =
  42. {
  43. {TW_TYPE_UNDEF,"TW_TYPE_UNDEF"},
  44. {TW_TYPE_BOOLCPP,"TW_TYPE_BOOLCPP"},
  45. {TW_TYPE_BOOL8,"TW_TYPE_BOOL8"},
  46. {TW_TYPE_BOOL16,"TW_TYPE_BOOL16"},
  47. {TW_TYPE_BOOL32,"TW_TYPE_BOOL32"},
  48. {TW_TYPE_CHAR,"TW_TYPE_CHAR"},
  49. {TW_TYPE_INT8,"TW_TYPE_INT8"},
  50. {TW_TYPE_UINT8,"TW_TYPE_UINT8"},
  51. {TW_TYPE_INT16,"TW_TYPE_INT16"},
  52. {TW_TYPE_UINT16,"TW_TYPE_UINT16"},
  53. {TW_TYPE_INT32,"TW_TYPE_INT32"},
  54. {TW_TYPE_UINT32,"TW_TYPE_UINT32"},
  55. {TW_TYPE_FLOAT,"TW_TYPE_FLOAT"},
  56. {TW_TYPE_DOUBLE,"TW_TYPE_DOUBLE"},
  57. {TW_TYPE_COLOR32,"TW_TYPE_COLOR32"},
  58. {TW_TYPE_COLOR3F,"TW_TYPE_COLOR3F"},
  59. {TW_TYPE_COLOR4F,"TW_TYPE_COLOR4F"},
  60. {TW_TYPE_CDSTRING,"TW_TYPE_CDSTRING"},
  61. {TW_TYPE_STDSTRING,"TW_TYPE_STDSTRING"},
  62. {TW_TYPE_QUAT4F,"TW_TYPE_QUAT4F"},
  63. {TW_TYPE_QUAT4D,"TW_TYPE_QUAT4D"},
  64. {TW_TYPE_DIR3F,"TW_TYPE_DIR3F"},
  65. {TW_TYPE_DIR3D,"TW_TYPE_DIR3D"}
  66. };
  67. }
  68. // BAR WRAPPERS
  69. void igl::ReTwBar::TwNewBar(const char *barName)
  70. {
  71. // double colon without anything in front of it means look for this in the
  72. // global namespace... I hope...
  73. this->bar = ::TwNewBar(barName);
  74. }
  75. int igl::ReTwBar::TwAddVarRW(
  76. const char *name,
  77. TwType type,
  78. void *var,
  79. const char *def,
  80. const bool record)
  81. {
  82. int ret = ::TwAddVarRW(this->bar,name,type,var,def);
  83. if(ret && record)
  84. {
  85. rw_items.push_back(ReTwRWItem(name,type,var));
  86. }
  87. return ret;
  88. }
  89. int igl::ReTwBar::TwAddVarCB(
  90. const char *name,
  91. TwType type,
  92. TwSetVarCallback setCallback,
  93. TwGetVarCallback getCallback,
  94. void *clientData,
  95. const char *def,
  96. const bool record)
  97. {
  98. int ret =
  99. ::TwAddVarCB(this->bar,name,type,setCallback,getCallback,clientData,def);
  100. if(ret && record)
  101. {
  102. cb_items.push_back(ReTwCBItem(name,type,setCallback,getCallback,clientData));
  103. }
  104. return ret;
  105. }
  106. int igl::ReTwBar::TwAddVarRO(
  107. const char *name,
  108. TwType type,
  109. void *var,
  110. const char *def)
  111. {
  112. int ret = ::TwAddVarRO(this->bar,name,type,var,def);
  113. // Read only variables are not recorded
  114. //if(ret)
  115. //{
  116. // rw_items.push_back(ReTwRWItem(name,type,var));
  117. //}
  118. return ret;
  119. }
  120. int igl::ReTwBar::TwAddButton(
  121. const char *name,
  122. TwButtonCallback buttonCallback,
  123. void *clientData,
  124. const char *def)
  125. {
  126. int ret =
  127. ::TwAddButton(this->bar,name,buttonCallback,clientData,def);
  128. // buttons are not recorded
  129. //if(ret)
  130. //{
  131. // cb_items.push_back(ReTwCBItem(name,type,setCallback,getCallback,clientData));
  132. //}
  133. return ret;
  134. }
  135. int igl::ReTwBar::TwSetParam(
  136. const char *varName,
  137. const char *paramName,
  138. TwParamValueType paramValueType,
  139. unsigned int inValueCount,
  140. const void *inValues)
  141. {
  142. // For now just pass these along
  143. return
  144. ::TwSetParam(
  145. this->bar,
  146. varName,
  147. paramName,
  148. paramValueType,
  149. inValueCount,
  150. inValues);
  151. }
  152. int igl::ReTwBar::TwGetParam(
  153. const char *varName,
  154. const char *paramName,
  155. TwParamValueType paramValueType,
  156. unsigned int outValueMaxCount,
  157. void *outValues)
  158. {
  159. return
  160. ::TwGetParam(
  161. this->bar,
  162. varName,
  163. paramName,
  164. paramValueType,
  165. outValueMaxCount,
  166. outValues);
  167. }
  168. int igl::ReTwBar::TwRefreshBar()
  169. {
  170. return ::TwRefreshBar(this->bar);
  171. }
  172. int igl::ReTwBar::TwTerminate()
  173. {
  174. return ::TwTerminate();
  175. }
  176. bool igl::ReTwBar::save(const char *file_name)
  177. {
  178. FILE * fp;
  179. if(file_name == NULL)
  180. {
  181. fp = stdout;
  182. }else
  183. {
  184. fp = fopen(file_name,"w");
  185. }
  186. if(fp == NULL)
  187. {
  188. printf("ERROR: not able to open %s for writing...\n",file_name);
  189. return false;
  190. }
  191. // Print all RW variables
  192. for(
  193. std::vector<ReTwRWItem>::iterator it = rw_items.begin();
  194. it != rw_items.end();
  195. it++)
  196. {
  197. const char * name = (*it).name.c_str();
  198. TwType type = (*it).type;
  199. void * var = (*it).var;
  200. fprintf(fp,"%s: %s\n",
  201. name,
  202. get_value_as_string(var,type).c_str());
  203. }
  204. char var[MAX_CB_VAR_SIZE];
  205. // Print all CB variables
  206. for(
  207. std::vector<ReTwCBItem>::iterator it = cb_items.begin();
  208. it != cb_items.end();
  209. it++)
  210. {
  211. const char * name = it->name.c_str();
  212. TwType type = it->type;
  213. //TwSetVarCallback setCallback = it->setCallback;
  214. TwGetVarCallback getCallback = it->getCallback;
  215. void * clientData = it->clientData;
  216. // I'm not sure how to do what I want to do. getCallback needs to be sure
  217. // that it can write to var. So var needs to point to a valid and big
  218. // enough chunk of memory
  219. getCallback(var,clientData);
  220. fprintf(fp,"%s: %s\n",
  221. name,
  222. get_value_as_string(var,type).c_str());
  223. }
  224. fprintf(fp,"\n");
  225. if(file_name != NULL)
  226. {
  227. fclose(fp);
  228. }
  229. // everything succeeded
  230. return true;
  231. }
  232. std::string igl::ReTwBar::get_value_as_string(
  233. void * var,
  234. TwType type)
  235. {
  236. std::stringstream sstr;
  237. switch(type)
  238. {
  239. case TW_TYPE_BOOLCPP:
  240. {
  241. sstr << "TW_TYPE_BOOLCPP" << " ";
  242. sstr << *(static_cast<bool*>(var));
  243. break;
  244. }
  245. case TW_TYPE_QUAT4D:
  246. {
  247. sstr << "TW_TYPE_QUAT4D" << " ";
  248. // Q: Why does casting to double* work? shouldn't I have to cast to
  249. // double**?
  250. double * q = static_cast<double*>(var);
  251. sstr << q[0] << " " << q[1] << " " << q[2] << " " << q[3];
  252. break;
  253. }
  254. case TW_TYPE_QUAT4F:
  255. {
  256. sstr << "TW_TYPE_QUAT4F" << " ";
  257. // Q: Why does casting to float* work? shouldn't I have to cast to
  258. // float**?
  259. float * q = static_cast<float*>(var);
  260. sstr << q[0] << " " << q[1] << " " << q[2] << " " << q[3];
  261. break;
  262. }
  263. case TW_TYPE_COLOR4F:
  264. {
  265. sstr << "TW_TYPE_COLOR4F" << " ";
  266. float * c = static_cast<float*>(var);
  267. sstr << c[0] << " " << c[1] << " " << c[2] << " " << c[3];
  268. break;
  269. }
  270. case TW_TYPE_COLOR3F:
  271. {
  272. sstr << "TW_TYPE_COLOR3F" << " ";
  273. float * c = static_cast<float*>(var);
  274. sstr << c[0] << " " << c[1] << " " << c[2];
  275. break;
  276. }
  277. case TW_TYPE_DIR3F:
  278. {
  279. sstr << "TW_TYPE_DIR3F" << " ";
  280. float * d = static_cast<float*>(var);
  281. sstr << d[0] << " " << d[1] << " " << d[2];
  282. break;
  283. }
  284. case TW_TYPE_BOOL32:
  285. {
  286. sstr << "TW_TYPE_BOOL32" << " ";
  287. sstr << *(static_cast<int*>(var));
  288. break;
  289. }
  290. case TW_TYPE_INT32:
  291. {
  292. sstr << "TW_TYPE_INT32" << " ";
  293. sstr << *(static_cast<int*>(var));
  294. break;
  295. }
  296. case TW_TYPE_FLOAT:
  297. {
  298. sstr << "TW_TYPE_FLOAT" << " ";
  299. sstr << *(static_cast<float*>(var));
  300. break;
  301. }
  302. case TW_TYPE_DOUBLE:
  303. {
  304. sstr << "TW_TYPE_DOUBLE" << " ";
  305. sstr << std::setprecision(15) << *(static_cast<double*>(var));
  306. break;
  307. }
  308. default:
  309. {
  310. std::map<TwType,std::pair<const char *,std::vector<TwEnumVal> > >::iterator iter =
  311. ReTw_custom_types.find(type);
  312. if(iter != ReTw_custom_types.end())
  313. {
  314. sstr << (*iter).second.first << " ";
  315. int enum_val = *(static_cast<int*>(var));
  316. // try find display name for enum value
  317. std::vector<TwEnumVal>::iterator eit = (*iter).second.second.begin();
  318. bool found = false;
  319. for(;eit<(*iter).second.second.end();eit++)
  320. {
  321. if(enum_val == eit->Value)
  322. {
  323. sstr << eit->Label;
  324. found = true;
  325. break;
  326. }
  327. }
  328. if(!found)
  329. {
  330. sstr << "ERROR_ENUM_VALUE_NOT_DEFINED";
  331. }
  332. }else
  333. {
  334. sstr << "ERROR_TYPE_NOT_SUPPORTED";
  335. }
  336. break;
  337. }
  338. }
  339. return sstr.str();
  340. }
  341. bool igl::ReTwBar::load(const char *file_name)
  342. {
  343. FILE * fp;
  344. fp = fopen(file_name,"r");
  345. if(fp == NULL)
  346. {
  347. printf("ERROR: not able to open %s for reading...\n",file_name);
  348. return false;
  349. }
  350. // go through file line by line
  351. char line[MAX_LINE];
  352. bool still_comments;
  353. char name[MAX_WORD];
  354. char type_str[MAX_WORD];
  355. char value_str[MAX_WORD];
  356. // line number
  357. int j = 0;
  358. bool finished = false;
  359. while(true)
  360. {
  361. // Eat comments
  362. still_comments = true;
  363. while(still_comments)
  364. {
  365. if(fgets(line,MAX_LINE,fp) == NULL)
  366. {
  367. finished = true;
  368. break;
  369. }
  370. // Blank lines and lines that begin with # are comments
  371. still_comments = (line[0] == '#' || line[0] == '\n');
  372. j++;
  373. }
  374. if(finished)
  375. {
  376. break;
  377. }
  378. sscanf(line,"%[^:]: %s %[^\n]",name,type_str,value_str);
  379. //printf("%s: %s %s\n",name, type_str,value_str);
  380. TwType type;
  381. if(!type_from_string(type_str,type))
  382. {
  383. printf("ERROR: %s type not found... Skipping...\n",type_str);
  384. continue;
  385. }
  386. set_value_from_string(name,type,value_str);
  387. }
  388. fclose(fp);
  389. // everything succeeded
  390. return true;
  391. }
  392. bool igl::ReTwBar::type_from_string(const char *type_str, TwType & type)
  393. {
  394. // first check default types
  395. for(int j = 0; j < RETW_NUM_DEFAULT_TYPE_STRINGS; j++)
  396. {
  397. if(strcmp(type_str,ReTwDefaultTypeStrings[j].type_str) == 0)
  398. {
  399. type = ReTwDefaultTypeStrings[j].type;
  400. return true;
  401. break;
  402. }
  403. }
  404. // then check custom types
  405. std::map<TwType,std::pair<const char *,std::vector<TwEnumVal> > >::iterator iter =
  406. ReTw_custom_types.begin();
  407. for(;iter != ReTw_custom_types.end(); iter++)
  408. {
  409. if(strcmp((*iter).second.first,type_str)==0)
  410. {
  411. type = (*iter).first;
  412. return true;
  413. }
  414. }
  415. return false;
  416. }
  417. bool igl::ReTwBar::set_value_from_string(
  418. const char * name,
  419. TwType type,
  420. const char * value_str)
  421. {
  422. void * value = NULL;
  423. // possible value slots
  424. int i;
  425. float v;
  426. double dv;
  427. float f[4];
  428. double d[4];
  429. bool b;
  430. // First try to get value from default types
  431. switch(type)
  432. {
  433. case TW_TYPE_BOOLCPP:
  434. {
  435. int ib;
  436. if(sscanf(value_str," %d",&ib) == 1)
  437. {
  438. b = ib!=0;
  439. value = &b;
  440. }else
  441. {
  442. printf("ERROR: Bad value format...\n");
  443. return false;
  444. }
  445. break;
  446. }
  447. case TW_TYPE_QUAT4D:
  448. //case TW_TYPE_COLOR4D:
  449. {
  450. if(sscanf(value_str," %lf %lf %lf %lf",&d[0],&d[1],&d[2],&d[3]) == 4)
  451. {
  452. value = &d;
  453. }else
  454. {
  455. printf("ERROR: Bad value format...\n");
  456. return false;
  457. }
  458. break;
  459. }
  460. case TW_TYPE_QUAT4F:
  461. case TW_TYPE_COLOR4F:
  462. {
  463. if(sscanf(value_str," %f %f %f %f",&f[0],&f[1],&f[2],&f[3]) == 4)
  464. {
  465. value = &f;
  466. }else
  467. {
  468. printf("ERROR: Bad value format...\n");
  469. return false;
  470. }
  471. break;
  472. }
  473. case TW_TYPE_COLOR3F:
  474. case TW_TYPE_DIR3F:
  475. {
  476. if(sscanf(value_str," %f %f %f",&f[0],&f[1],&f[2]) == 3)
  477. {
  478. value = &f;
  479. }else
  480. {
  481. printf("ERROR: Bad value format...\n");
  482. return false;
  483. }
  484. break;
  485. }
  486. case TW_TYPE_BOOL32:
  487. case TW_TYPE_INT32:
  488. {
  489. if(sscanf(value_str," %d",&i) == 1)
  490. {
  491. value = &i;
  492. }else
  493. {
  494. printf("ERROR: Bad value format...\n");
  495. return false;
  496. }
  497. break;
  498. }
  499. case TW_TYPE_FLOAT:
  500. {
  501. if(sscanf(value_str," %f",&v) == 1)
  502. {
  503. value = &v;
  504. }else
  505. {
  506. printf("ERROR: Bad value format...\n");
  507. return false;
  508. }
  509. break;
  510. }
  511. case TW_TYPE_DOUBLE:
  512. {
  513. if(sscanf(value_str," %lf",&dv) == 1)
  514. {
  515. value = &dv;
  516. }else
  517. {
  518. printf("ERROR: Bad value format...\n");
  519. return false;
  520. }
  521. break;
  522. }
  523. default:
  524. // Try to find type in custom enum types
  525. std::map<TwType,std::pair<const char *,std::vector<TwEnumVal> > >::iterator iter =
  526. ReTw_custom_types.find(type);
  527. if(iter != ReTw_custom_types.end())
  528. {
  529. std::vector<TwEnumVal>::iterator eit = (*iter).second.second.begin();
  530. bool found = false;
  531. for(;eit<(*iter).second.second.end();eit++)
  532. {
  533. if(strcmp(value_str,eit->Label) == 0)
  534. {
  535. i = eit->Value;
  536. value = &i;
  537. found = true;
  538. break;
  539. }
  540. }
  541. if(!found)
  542. {
  543. printf("ERROR_ENUM_VALUE_NOT_DEFINED");
  544. }
  545. }else
  546. {
  547. printf("ERROR_TYPE_NOT_SUPPORTED\n");
  548. }
  549. break;
  550. }
  551. // Find variable based on name
  552. // First look in RW items
  553. bool item_found = false;
  554. for(
  555. std::vector<ReTwRWItem>::iterator it = rw_items.begin();
  556. it != rw_items.end();
  557. it++)
  558. {
  559. if(it->name == name)
  560. {
  561. void * var = it->var;
  562. switch(type)
  563. {
  564. case TW_TYPE_BOOLCPP:
  565. {
  566. bool * bvar = static_cast<bool*>(var);
  567. bool * bvalue = static_cast<bool*>(value);
  568. *bvar = *bvalue;
  569. break;
  570. }
  571. case TW_TYPE_QUAT4D:
  572. //case TW_TYPE_COLOR4D:
  573. {
  574. double * dvar = static_cast<double*>(var);
  575. double * dvalue = static_cast<double*>(value);
  576. dvar[0] = dvalue[0];
  577. dvar[1] = dvalue[1];
  578. dvar[2] = dvalue[2];
  579. dvar[3] = dvalue[3];
  580. break;
  581. }
  582. case TW_TYPE_QUAT4F:
  583. case TW_TYPE_COLOR4F:
  584. {
  585. float * fvar = static_cast<float*>(var);
  586. float * fvalue = static_cast<float*>(value);
  587. fvar[0] = fvalue[0];
  588. fvar[1] = fvalue[1];
  589. fvar[2] = fvalue[2];
  590. fvar[3] = fvalue[3];
  591. break;
  592. }
  593. case TW_TYPE_COLOR3F:
  594. case TW_TYPE_DIR3F:
  595. {
  596. float * fvar = static_cast<float*>(var);
  597. float * fvalue = static_cast<float*>(value);
  598. fvar[0] = fvalue[0];
  599. fvar[1] = fvalue[1];
  600. fvar[2] = fvalue[2];
  601. break;
  602. }
  603. case TW_TYPE_BOOL32:
  604. case TW_TYPE_INT32:
  605. {
  606. int * ivar = static_cast<int*>(var);
  607. int * ivalue = static_cast<int*>(value);
  608. *ivar = *ivalue;
  609. break;
  610. }
  611. case TW_TYPE_FLOAT:
  612. {
  613. float * fvar = static_cast<float*>(var);
  614. float * fvalue = static_cast<float*>(value);
  615. *fvar = *fvalue;
  616. break;
  617. }
  618. case TW_TYPE_DOUBLE:
  619. {
  620. double * dvar = static_cast<double*>(var);
  621. double * fvalue = static_cast<double*>(value);
  622. *dvar = *fvalue;
  623. break;
  624. }
  625. default:
  626. // Try to find type in custom enum types
  627. std::map<TwType,std::pair<const char *,std::vector<TwEnumVal> > >::iterator iter =
  628. ReTw_custom_types.find(type);
  629. if(iter != ReTw_custom_types.end())
  630. {
  631. int * ivar = static_cast<int*>(var);
  632. std::vector<TwEnumVal>::iterator eit = (*iter).second.second.begin();
  633. bool found = false;
  634. for(;eit<(*iter).second.second.end();eit++)
  635. {
  636. if(strcmp(value_str,eit->Label) == 0)
  637. {
  638. *ivar = eit->Value;
  639. found = true;
  640. break;
  641. }
  642. }
  643. if(!found)
  644. {
  645. printf("ERROR_ENUM_VALUE_NOT_DEFINED");
  646. }
  647. }else
  648. {
  649. printf("ERROR_TYPE_NOT_SUPPORTED\n");
  650. }
  651. break;
  652. }
  653. item_found = true;
  654. break;
  655. }
  656. }
  657. // Try looking in CB items
  658. if(!item_found)
  659. {
  660. for(
  661. std::vector<ReTwCBItem>::iterator it = cb_items.begin();
  662. it != cb_items.end();
  663. it++)
  664. {
  665. if(it->name==name)
  666. {
  667. it->setCallback(value,it->clientData);
  668. item_found = true;
  669. break;
  670. }
  671. }
  672. }
  673. if(!item_found)
  674. {
  675. printf("ERROR: item not found\n");
  676. }
  677. return true;
  678. }