Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.93 KB | None | 0 0
  1. EXPORT GameApi::BO GameApi::BooleanOps::cube(GameApi::EveryApi &ev,
  2. float start_x, float end_x,
  3. float start_y, float end_y,
  4. float start_z, float end_z,
  5. int split_x, int split_y)
  6. {
  7. P mesh = ev.polygon_api.cube(start_x, end_x, start_y, end_y, start_z, end_z);
  8. P mesh2 = ev.polygon_api.splitquads(mesh, split_x, split_y);
  9.  
  10. O bools = ev.volume_api.cube(start_x, end_x, start_y, end_y, start_z, end_z);
  11. FD fd = ev.dist_api.cube(start_x, end_x, start_y, end_y, start_z, end_z);
  12. //CT cutter = ev.cutter_api.distance_cut(fd);
  13. return create_bo(mesh2, bools, fd);
  14. }
  15. EXPORT GameApi::BO GameApi::BooleanOps::sphere(GameApi::EveryApi &ev, PT center, float radius, int numfaces1, int numfaces2)
  16. {
  17. P mesh = ev.polygon_api.sphere(center, radius, numfaces1, numfaces2);
  18. O bools = ev.volume_api.sphere(center, radius);
  19. FD fd = ev.dist_api.sphere(center, radius);
  20. return create_bo(mesh, bools, fd);
  21. }
  22. EXPORT GameApi::BO GameApi::BooleanOps::or_elem(GameApi::EveryApi &ev, GameApi::BO obj, GameApi::BO obj2)
  23. {
  24. ::EnvImpl *env = ::EnvImpl::Environment(&e);
  25. BO_Impl obj_i = env->boolean_ops[obj.id];
  26. BO_Impl obj2_i = env->boolean_ops[obj2.id];
  27. P mesh = ev.polygon_api.or_elem(obj_i.mesh, obj2_i.mesh);
  28. O bools = ev.volume_api.max_op(obj_i.bools, obj2_i.bools);
  29. FD fd = ev.dist_api.min(obj_i.fd, obj2_i.fd);
  30. return create_bo(mesh, bools, fd);
  31. }
  32. EXPORT GameApi::BO GameApi::BooleanOps::and_not(GameApi::EveryApi &ev, GameApi::BO obj, GameApi::BO obj2)
  33. {
  34. ::EnvImpl *env = ::EnvImpl::Environment(&e);
  35. BO_Impl obj_i = env->boolean_ops[obj.id];
  36. BO_Impl obj2_i = env->boolean_ops[obj2.id];
  37.  
  38. CT cutter = ev.cutter_api.distance_cut(obj_i.fd);
  39. CT cutter2 = ev.cutter_api.distance_cut(obj2_i.fd);
  40. P mesh = ev.polygon_api.and_not_elem(ev, obj2_i.mesh, obj_i.mesh,
  41. obj2_i.bools, obj_i.bools,
  42. cutter2, cutter);
  43. //P mesh2 = ev.polygon_api.tri_to_quad(mesh);
  44. O bools = ev.volume_api.andnot_op(obj_i.bools, obj2_i.bools);
  45. FD fd = ev.dist_api.and_not(obj_i.fd, obj2_i.fd);
  46. return create_bo(mesh, bools, fd);
  47. }
  48.  
  49. EXPORT GameApi::BO GameApi::BooleanOps::intersect(GameApi::EveryApi &ev, GameApi::BO obj, GameApi::BO obj2)
  50. {
  51. ::EnvImpl *env = ::EnvImpl::Environment(&e);
  52. BO_Impl obj_i = env->boolean_ops[obj.id];
  53. BO_Impl obj2_i = env->boolean_ops[obj2.id];
  54.  
  55. CT cutter = ev.cutter_api.distance_cut(obj_i.fd);
  56. CT cutter2 = ev.cutter_api.distance_cut(obj2_i.fd);
  57. P mesh = ev.polygon_api.intersect(ev, obj_i.mesh, obj2_i.mesh,
  58. obj_i.bools, obj2_i.bools,
  59. cutter, cutter2);
  60. O bools = ev.volume_api.min_op(obj_i.bools, obj2_i.bools);
  61. FD fd = ev.dist_api.max(obj_i.fd, obj2_i.fd);
  62. return create_bo(mesh, bools, fd);
  63. }
  64.  
  65. class DistanceCut : public Cutter
  66. {
  67. public:
  68. DistanceCut(DistanceRenderable *dr) : dr(dr) {}
  69. std::vector<Point> cut(Point p1, Point p2) const
  70. {
  71. float d1 = dr->distance(p1);
  72. float d2 = dr->distance(p2);
  73. if (d1<d2)
  74. std::swap(p1,p2);
  75. Point p = p1;
  76. Vector v = (p2-p1);
  77. float dd = v.Dist();
  78. v /= dd;
  79. std::vector<Point> vec;
  80. //int count = 0;
  81. float pos = 0.0;
  82. while(1) {
  83. float d = dr->distance(p);
  84. //std::cout << "Dist: " << d << std::endl;
  85. //std::cout << "Pos: " << pos << std::endl;
  86. if (pos>1.0) { vec.push_back(p2); break; }//std::vector<Point>{ p2 };
  87. if (fabs(d)<0.0001) { vec.push_back(p); break; }
  88. //if (count>30) return Point(0.0,0.0,0.0);
  89. // p+=v * d;
  90. pos+=fabs(d/dd);
  91. //if (pos>=0.0 && pos<=1.0)
  92. p = (1.0-pos)*Vector(p1)+pos*Vector(p2);
  93. //count++;
  94. }
  95. //if (pos<0.0 ||pos>1.0)
  96. // std::cout << p1 << " " << p2 << ":" << p << std::endl;
  97. return vec;
  98. }
  99. private:
  100. DistanceRenderable *dr;
  101. };
  102.  
  103. EXPORT GameApi::CT GameApi::CutterApi::distance_cut(FD dist1)
  104. {
  105. DistanceRenderable *dist = find_distance(e, dist1);
  106. return add_cutter(e, new DistanceCut(dist));
  107. }
  108.  
  109. class CutFaces : public ForwardFaceCollection
  110. {
  111. public:
  112. CutFaces(FaceCollection *i, VolumeObject *oo, Cutter *cut) : ForwardFaceCollection(*i), i(i), oo(oo), cut(cut) { cut_them(); compress(); }
  113.  
  114. void cut_them()
  115. {
  116. int f = i->NumFaces();
  117. for(int ii=0;ii<f; ii++)
  118. {
  119. faces.push_back(std::vector<Point>());
  120. int p = i->NumPoints(ii);
  121. //std::cout << p << std::endl;
  122. std::vector<Point> &ref = faces[faces.size()-1];
  123. //std::cout << "start" << std::endl;
  124. for(int jj=0;jj<p;jj++)
  125. {
  126. int jjj1 = jj;
  127. while(jjj1<0) jjj1+=p;
  128. while(jjj1>=p) jjj1-=p;
  129. int jjj2 = jjj1+1;
  130. while(jjj2<0) jjj2+=p;
  131. while(jjj2>=p) jjj2-=p;
  132. Point p1 = i->FacePoint(ii, jjj1);
  133. Point p2 = i->FacePoint(ii, jjj2);
  134. bool b1 = oo->Inside(p1);
  135. bool b2 = oo->Inside(p2);
  136.  
  137. if (jj==0) { ref.push_back(p1); }
  138.  
  139. if (!b1 && !b2) {
  140. //std::cout << "!b1 && !b2" << std::endl;
  141. //ref.push_back(p1);
  142. ref.push_back(p2);
  143. }
  144. if (!b1 && b2) {
  145. //std::cout << "!b1 && b2" << std::endl;
  146. Point prev = p1;
  147. Point curr = p2;
  148. for(int h=0;h<p;h++)
  149. {
  150. int jjj3 = jjj2+h;
  151. while(jjj3<0) jjj3+=p;
  152. while(jjj3>=p) jjj3-=p;
  153.  
  154. curr = i->FacePoint(ii, jjj3);
  155. bool b3 = oo->Inside(curr);
  156. if (!b3) break;
  157. prev = curr;
  158. }
  159. std::vector<Point> c0 = cut->cut(p1,p2);
  160. std::vector<Point> c1 = cut->cut(prev,curr);
  161. //ref.push_back(p1);
  162. ref.push_back(c0[0]);
  163. ref.push_back(c1[0]);
  164. ref.push_back(curr);
  165. }
  166. #if 1
  167. if (b1 && !b2) {
  168. //std::cout << "b1 && !b2" << std::endl;
  169. std::vector<Point> c0 = cut->cut(p1,p2);
  170. //ref.push_back(c0[0]);
  171. ref.push_back(p2);
  172. }
  173. #endif
  174. //if (b1 && b2)
  175. // {
  176. // ref.push_back(p1);
  177. // ref.push_back(p2);
  178. // }
  179.  
  180.  
  181.  
  182. }
  183. }
  184. }
  185. void compress()
  186. {
  187. int f = faces.size();
  188. int count = 0;
  189. for(int ii=0;ii<f; ii++)
  190. {
  191. int p = faces[ii].size();
  192. if (p==0 ||p==1 ||p==2) continue;
  193. faces2.push_back(std::vector<Point>());
  194. #if 1
  195. Point prev;
  196. for(int i=0;i<p;i++)
  197. {
  198. if ((prev-faces[ii][i]).Dist()>0.001)
  199. faces2[count].push_back(faces[ii][i]);
  200. prev = faces[ii][i];
  201. }
  202. #endif
  203. // faces2[count].push_back(faces2[count][0]);
  204. count++;
  205. }
  206. }
  207.  
  208. virtual int NumFaces() const { return faces2.size(); }
  209. virtual int NumPoints(int face) const { return faces2[face].size(); }
  210. virtual Point FacePoint(int face, int point) const
  211. {
  212. return faces2[face][point];
  213. }
  214. virtual Vector PointNormal(int face, int point) const
  215. {
  216. Vector v(0.0, 0.0, 0.0);
  217. return v;
  218. }
  219. virtual float Attrib(int face, int point, int id) const { return 0.0; }
  220. virtual int AttribI(int face, int point, int id) const { return 0; }
  221. virtual unsigned int Color(int face, int point) const
  222. {
  223. return 0xffffffff;
  224. }
  225. virtual Point2d TexCoord(int face, int point) const
  226. {
  227. Point2d p;
  228. p.x = 0.0;
  229. p.y = 0.0;
  230. return p;
  231. }
  232.  
  233.  
  234. private:
  235. FaceCollection *i;
  236. VolumeObject *oo;
  237. Cutter *cut;
  238. std::vector<std::vector<Point> > faces;
  239. std::vector<std::vector<Point> > faces2;
  240. };
  241.  
  242. EXPORT GameApi::P GameApi::PolygonApi::cut_faces(P p, O o, CT cutter)
  243. {
  244. FaceCollection *i = find_facecoll(e, p);
  245. VolumeObject *oo = find_volume(e, o);
  246. Cutter *cut = find_cutter(e, cutter);
  247. FaceCollection *coll = new CutFaces(i, oo, cut);
  248. return add_polygon(e, coll, 1);
  249. }
  250. EXPORT GameApi::P GameApi::PolygonApi::intersect(EveryApi &ev, P p1, P p2,
  251. O o1, O o2,
  252. CT cutter1, CT cutter2)
  253. {
  254. O o1_n = ev.volume_api.not_op(o1);
  255. O o2_n = ev.volume_api.not_op(o2);
  256.  
  257. P p1_cut = cut_faces(p1, o2_n, cutter2);
  258. TS ts_1 = ev.ts_api.from_poly(p1_cut);
  259. P p_1 = ev.ts_api.to_poly(ts_1);
  260.  
  261. P p2_cut = cut_faces(p2, o1_n, cutter1);
  262. TS ts_2 = ev.ts_api.from_poly(p2_cut);
  263. P p_2_ = ev.ts_api.to_poly(ts_2);
  264.  
  265. P or_1 = or_elem(p_1, p_2_);
  266. return or_1;
  267.  
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement