Index.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /******************************************************************************/
  2. #include "stdafx.h"
  3. namespace EE{
  4. /******************************************************************************/
  5. void IndexGroup ::changeVal(Int from, Int to) {REP( num )if(elm[i]==from ) elm[i]=to;}
  6. void IndexPtrGroup::changeVal(Ptr from, Ptr to) {REP( num )if(elm[i]==from ) elm[i]=to;}
  7. void IndexGroup :: remVal(Int value ) {for(Int i=0; i<num; )if(elm[i]==value) elm[i]=elm[--num]; else i++;}
  8. void IndexPtrGroup:: remVal(Ptr value ) {for(Int i=0; i<num; )if(elm[i]==value) elm[i]=elm[--num]; else i++;}
  9. void IndexGroup :: subVal(Int value ) {for(Int i=0; i<num; )if(elm[i]==value){elm[i]=elm[--num]; break;}else i++;}
  10. void IndexPtrGroup:: subVal(Ptr value ) {for(Int i=0; i<num; )if(elm[i]==value){elm[i]=elm[--num]; break;}else i++;}
  11. void IndexGroup :: subElm(Int i ) {if (InRange(i, num) ) elm[i]=elm[--num];}
  12. void IndexPtrGroup:: subElm(Int i ) {if (InRange(i, num) ) elm[i]=elm[--num];}
  13. /******************************************************************************/
  14. Index& Index::del()
  15. {
  16. Free(group);
  17. Free(groups_elm);
  18. Free(elm_group);
  19. Zero(T); return T;
  20. }
  21. IndexPtr& IndexPtr::del()
  22. {
  23. Free(group);
  24. Free(groups_elm);
  25. Zero(T); return T;
  26. }
  27. Index& Index::create(Int groups, Int elms_max)
  28. {
  29. del();
  30. AllocZero(group , groups );
  31. Alloc (groups_elm, elms_max);
  32. Alloc ( elm_group, elms_max);
  33. T.groups =groups;
  34. T.elms_max=elms_max;
  35. return T;
  36. }
  37. IndexPtr& IndexPtr::create(Int groups)
  38. {
  39. del();
  40. AllocZero(group, groups);
  41. T.groups=groups;
  42. return T;
  43. }
  44. Index& Index::reset()
  45. {
  46. ZeroN(group, groups);
  47. if(!elms_max)
  48. {
  49. Free(elm_group);
  50. Free(groups_elm);
  51. }
  52. elms=group_elms_max=0;
  53. return T;
  54. }
  55. IndexPtr& IndexPtr::reset()
  56. {
  57. ZeroN(group, groups);
  58. Free (groups_elm);
  59. elms=group_elms_max=0;
  60. return T;
  61. }
  62. Index& Index::set(Int *elm_group)
  63. {
  64. // create groups_elm array
  65. if(!groups_elm)
  66. {
  67. Int elms=0; IndexGroup *ig=group;
  68. REP(groups)elms+=(ig++)->num;
  69. Alloc(groups_elm, elms);
  70. }
  71. // set group element offsets
  72. IndexGroup *ig=group;
  73. elms=0; REP(groups)
  74. {
  75. if(ig->num>group_elms_max)group_elms_max=ig->num;
  76. ig->elm=&groups_elm[elms];
  77. elms+=ig->num;
  78. ig->num=0;
  79. ig++;
  80. }
  81. // optionally fill group_elm array
  82. if(!elm_group)elm_group=T.elm_group;
  83. if( elm_group)FREP(elms)addElm(*elm_group++, i);
  84. return T;
  85. }
  86. IndexPtr& IndexPtr::set()
  87. {
  88. // create group_elm array
  89. if(!groups_elm)
  90. {
  91. Int elms=0; IndexPtrGroup *ig=group;
  92. REP(groups)elms+=(ig++)->num;
  93. Alloc(groups_elm, elms);
  94. }
  95. // set group element offsets
  96. IndexPtrGroup *ig=group;
  97. elms=0; REP(groups)
  98. {
  99. if(ig->num>group_elms_max)group_elms_max=ig->num;
  100. ig->elm=&groups_elm[elms];
  101. elms+=ig->num;
  102. ig->num=0;
  103. ig++;
  104. }
  105. return T;
  106. }
  107. /******************************************************************************/
  108. Bool ElmIs(C MemPtr<Bool> &is, Int i) {return InRange(i, is) && is[i];}
  109. Int CountIs(C MemPtr<Bool> &is)
  110. {
  111. Int n=0; REPA(is)n+=is[i];
  112. return n;
  113. }
  114. void CreateIs(MemPtr<Bool> is, C MemPtr<Int> &selection, Int elms)
  115. {
  116. is.setNum(elms); REPAO(is)=false; REPA(selection){Int index=selection[i]; if(InRange(index, is))is[index]=true;}
  117. }
  118. void CreateIsNot(MemPtr<Bool> is, C MemPtr<Int> &selection, Int elms)
  119. {
  120. is.setNum(elms); REPAO(is)=true; REPA(selection){Int index=selection[i]; if(InRange(index, is))is[index]=false;}
  121. }
  122. void CreateFaceIs(MemPtr<Bool> tri_is, MemPtr<Bool> quad_is, C MemPtr<Int> &faces, Int tris, Int quads)
  123. {
  124. tri_is.setNum( tris); REPAO( tri_is)=false;
  125. quad_is.setNum(quads); REPAO(quad_is)=false;
  126. REPA(faces)
  127. {
  128. UInt f=faces[i];
  129. if(f&SIGN_BIT){f^=SIGN_BIT; if(InRange(f, quad_is))quad_is[f]=true;}
  130. else { if(InRange(f, tri_is)) tri_is[f]=true;}
  131. }
  132. }
  133. void CreateFaceIsNot(MemPtr<Bool> tri_is, MemPtr<Bool> quad_is, C MemPtr<Int> &faces, Int tris, Int quads)
  134. {
  135. tri_is.setNum( tris); REPAO( tri_is)=true;
  136. quad_is.setNum(quads); REPAO(quad_is)=true;
  137. REPA(faces)
  138. {
  139. UInt f=faces[i];
  140. if(f&SIGN_BIT){f^=SIGN_BIT; if(InRange(f, quad_is))quad_is[f]=false;}
  141. else { if(InRange(f, tri_is)) tri_is[f]=false;}
  142. }
  143. }
  144. /******************************************************************************/
  145. void SetFaceIndex(Ptr data, C VecI *tri, Int tris, C VecI4 *quad, Int quads, Bool bit16)
  146. {
  147. if(bit16)
  148. {
  149. Copy32To16(data, tri, tris*3);
  150. VecUS *t=(VecUS*)data+tris; REP(quads){(t++)->set(quad->x, quad->y, quad->w); (t++)->set(quad->w, quad->y, quad->z); quad++;}
  151. }else
  152. {
  153. Copy32To32(data, tri, tris*3);
  154. VecI *t=(VecI*)data+tris; REP(quads){(t++)->set(quad->x, quad->y, quad->w); (t++)->set(quad->w, quad->y, quad->z); quad++;}
  155. }
  156. }
  157. /******************************************************************************/
  158. void SetRemap(MemPtr<Int> remap, C MemPtr<Bool> &is, Int elms)
  159. {
  160. remap.setNum(elms);
  161. Int full=Min(remap.elms(), is.elms()), i=0, index=0;
  162. for(; i< full ; i++)remap[i]=(is[i] ? index++ : -1);
  163. for(; i<remap.elms(); i++)remap[i]= -1 ;
  164. }
  165. void IndRemap(C MemPtr<Int> &remap, Int *ind, Int elms)
  166. {
  167. for(; elms-->0; ind++)*ind=(InRange(*ind, remap) ? remap[*ind] : -1);
  168. }
  169. /******************************************************************************/
  170. Bool IndSave(File &f, CPtr ind, Int inds, Int elms)
  171. {
  172. Int bytes=4;
  173. if(elms>0)
  174. {
  175. if(elms<=0x00000100)bytes=1;else
  176. if(elms<=0x00010000)bytes=2;else
  177. if(elms<=0x01000000)bytes=3;
  178. }
  179. Int size=inds*bytes;
  180. CPtr data=ind;
  181. Memt<Byte> temp;
  182. if(bytes!=4)
  183. {
  184. data=temp.setNum(size).data();
  185. switch(bytes)
  186. {
  187. case 3: Copy32To24(temp.data(), ind, inds); break;
  188. case 2: Copy32To16(temp.data(), ind, inds); break;
  189. case 1: Copy32To8 (temp.data(), ind, inds); break;
  190. }
  191. }
  192. f.putByte(bytes*8); // save as bits
  193. f.put(data, size);
  194. return f.ok();
  195. }
  196. Bool IndLoad(File &f, Ptr ind, Int inds)
  197. {
  198. Int bytes=f.getByte()/8,
  199. size=inds*bytes;
  200. Ptr data=ind;
  201. Memt<Byte> temp; if(bytes!=4)data=temp.setNum(size).data();
  202. f.getFast(data, size);
  203. switch(bytes)
  204. {
  205. case 4: break;
  206. case 3: Copy24To32(ind, data, inds); break;
  207. case 2: Copy16To32(ind, data, inds); break;
  208. case 1: Copy8To32 (ind, data, inds); break;
  209. default: return false;
  210. }
  211. return f.ok();
  212. }
  213. /******************************************************************************/
  214. }
  215. /******************************************************************************/