brushunion.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. #include "csg.h"
  2. vec_t g_BrushUnionThreshold = DEFAULT_BRUSH_UNION_THRESHOLD;
  3. static Winding* NewWindingFromPlane(const brushhull_t* const hull, const int planenum)
  4. {
  5. Winding* winding;
  6. Winding* front;
  7. Winding* back;
  8. bface_t* face;
  9. plane_t* plane;
  10. plane = &g_mapplanes[planenum];
  11. winding = new Winding(plane->normal, plane->dist);
  12. for (face = hull->faces; face; face = face->next)
  13. {
  14. plane = &g_mapplanes[face->planenum];
  15. winding->Clip(plane->normal, plane->dist, &front, &back);
  16. delete winding;
  17. if (front)
  18. {
  19. delete front;
  20. }
  21. if (back)
  22. {
  23. winding = back;
  24. }
  25. else
  26. {
  27. Developer(DEVELOPER_LEVEL_ERROR, "NewFaceFromPlane returning NULL");
  28. return NULL;
  29. }
  30. }
  31. return winding;
  32. }
  33. static void AddFaceToList(bface_t** head, bface_t* newface)
  34. {
  35. hlassert(newface);
  36. hlassert(newface->w);
  37. if (!*head)
  38. {
  39. *head = newface;
  40. return;
  41. }
  42. else
  43. {
  44. bface_t* node = *head;
  45. while (node->next)
  46. {
  47. node = node->next;
  48. }
  49. node->next = newface;
  50. newface->next = NULL;
  51. }
  52. }
  53. static int NumberOfHullFaces(const brushhull_t* const hull)
  54. {
  55. int x;
  56. bface_t* face;
  57. if (!hull->faces)
  58. {
  59. return 0;
  60. }
  61. for (x = 0, face = hull->faces; face; face = face->next, x++)
  62. { // counter
  63. }
  64. return x;
  65. }
  66. // Returns false if union of brushes is obviously zero
  67. static void AddPlaneToUnion(brushhull_t* hull, const int planenum)
  68. {
  69. bool need_new_face = false;
  70. bface_t* new_face_list;
  71. bface_t* face;
  72. bface_t* next;
  73. plane_t* split;
  74. Winding* front;
  75. Winding* back;
  76. new_face_list = NULL;
  77. next = NULL;
  78. hlassert(hull);
  79. if (!hull->faces)
  80. {
  81. return;
  82. }
  83. hlassert(hull->faces->w);
  84. for (face = hull->faces; face; face = next)
  85. {
  86. hlassert(face->w);
  87. next = face->next;
  88. // Duplicate plane, ignore
  89. if (face->planenum == planenum)
  90. {
  91. AddFaceToList(&new_face_list, CopyFace(face));
  92. continue;
  93. }
  94. split = &g_mapplanes[planenum];
  95. face->w->Clip(split->normal, split->dist, &front, &back);
  96. if (front)
  97. {
  98. delete front;
  99. need_new_face = true;
  100. if (back)
  101. { // Intersected the face
  102. delete face->w;
  103. face->w = back;
  104. AddFaceToList(&new_face_list, CopyFace(face));
  105. }
  106. }
  107. else
  108. {
  109. // Completely missed it, back is identical to face->w so it is destroyed
  110. if (back)
  111. {
  112. delete back;
  113. AddFaceToList(&new_face_list, CopyFace(face));
  114. }
  115. }
  116. hlassert(face->w);
  117. }
  118. FreeFaceList(hull->faces);
  119. hull->faces = new_face_list;
  120. if (need_new_face && (NumberOfHullFaces(hull) > 2))
  121. {
  122. Winding* new_winding = NewWindingFromPlane(hull, planenum);
  123. if (new_winding)
  124. {
  125. bface_t* new_face = (bface_t*)Alloc(sizeof(bface_t));
  126. new_face->planenum = planenum;
  127. new_face->w = new_winding;
  128. new_face->next = hull->faces;
  129. hull->faces = new_face;
  130. }
  131. }
  132. }
  133. static vec_t CalculateSolidVolume(const brushhull_t* const hull)
  134. {
  135. // calculate polyhedron origin
  136. // subdivide face winding into triangles
  137. // for each face
  138. // calculate volume of triangle of face to origin
  139. // add subidivided volume chunk to total
  140. int x = 0;
  141. vec_t volume = 0.0;
  142. vec_t inverse;
  143. vec3_t midpoint = { 0.0, 0.0, 0.0 };
  144. bface_t* face;
  145. for (face = hull->faces; face; face = face->next, x++)
  146. {
  147. vec3_t facemid;
  148. face->w->getCenter(facemid);
  149. VectorAdd(midpoint, facemid, midpoint);
  150. Developer(DEVELOPER_LEVEL_MESSAGE, "Midpoint for face %d is %f %f %f\n", x, facemid[0], facemid[1], facemid[2]);
  151. }
  152. inverse = 1.0 / x;
  153. VectorScale(midpoint, inverse, midpoint);
  154. Developer(DEVELOPER_LEVEL_MESSAGE, "Midpoint for hull is %f %f %f\n", midpoint[0], midpoint[1], midpoint[2]);
  155. for (face = hull->faces; face; face = face->next, x++)
  156. {
  157. plane_t* plane = &g_mapplanes[face->planenum];
  158. vec_t area = face->w->getArea();
  159. vec_t dist = DotProduct(plane->normal, midpoint);
  160. dist -= plane->dist;
  161. dist = fabs(dist);
  162. volume += area * dist / 3.0;
  163. }
  164. Developer(DEVELOPER_LEVEL_MESSAGE, "Volume for brush is %f\n", volume);
  165. return volume;
  166. }
  167. static void DumpHullWindings(const brushhull_t* const hull)
  168. {
  169. int x = 0;
  170. bface_t* face;
  171. for (face = hull->faces; face; face = face->next)
  172. {
  173. Developer(DEVELOPER_LEVEL_MEGASPAM, "Winding %d\n", x++);
  174. face->w->Print();
  175. Developer(DEVELOPER_LEVEL_MEGASPAM, "\n");
  176. }
  177. }
  178. static bool isInvalidHull(const brushhull_t* const hull)
  179. {
  180. int x = 0;
  181. bface_t* face;
  182. vec3_t mins = { 99999.0, 99999.0, 99999.0 };
  183. vec3_t maxs = { -99999.0, -99999.0, -99999.0 };
  184. for (face = hull->faces; face; face = face->next)
  185. {
  186. unsigned int y;
  187. Winding* winding = face->w;
  188. for (y = 0; y < winding->m_NumPoints; y++)
  189. {
  190. VectorCompareMinimum(mins, winding->m_Points[y], mins);
  191. VectorCompareMaximum(maxs, winding->m_Points[y], maxs);
  192. }
  193. }
  194. for (x = 0; x < 3; x++)
  195. {
  196. if ((mins[x] < (-BOGUS_RANGE / 2)) || (maxs[x] > (BOGUS_RANGE / 2)))
  197. {
  198. return true;
  199. }
  200. }
  201. return false;
  202. }
  203. void CalculateBrushUnions(const int brushnum)
  204. {
  205. int bn, hull;
  206. brush_t* b1;
  207. brush_t* b2;
  208. brushhull_t* bh1;
  209. brushhull_t* bh2;
  210. entity_t* e;
  211. b1 = &g_mapbrushes[brushnum];
  212. e = &g_entities[b1->entitynum];
  213. for (hull = 0; hull < 1 /* NUM_HULLS */ ; hull++)
  214. {
  215. bh1 = &b1->hulls[hull];
  216. if (!bh1->faces) // Skip it if it is not in this hull
  217. {
  218. continue;
  219. }
  220. for (bn = brushnum + 1; bn < e->numbrushes; bn++)
  221. { // Only compare if b2 > b1, tests are communitive
  222. b2 = &g_mapbrushes[e->firstbrush + bn];
  223. bh2 = &b2->hulls[hull];
  224. if (!bh2->faces) // Skip it if it is not in this hull
  225. {
  226. continue;
  227. }
  228. if (b1->contents != b2->contents)
  229. {
  230. continue; // different contents, ignore
  231. }
  232. Developer(DEVELOPER_LEVEL_SPAM, "Processing hull %d brush %d and brush %d\n", hull, brushnum, bn);
  233. {
  234. brushhull_t union_hull;
  235. bface_t* face;
  236. union_hull.bounds = bh1->bounds;
  237. union_hull.faces = CopyFaceList(bh1->faces);
  238. for (face = bh2->faces; face; face = face->next)
  239. {
  240. AddPlaneToUnion(&union_hull, face->planenum);
  241. }
  242. // union was clipped away (no intersection)
  243. if (!union_hull.faces)
  244. {
  245. continue;
  246. }
  247. if (g_developer >= DEVELOPER_LEVEL_MESSAGE)
  248. {
  249. Log("\nUnion windings\n");
  250. DumpHullWindings(&union_hull);
  251. Log("\nBrush %d windings\n", brushnum);
  252. DumpHullWindings(bh1);
  253. Log("\nBrush %d windings\n", bn);
  254. DumpHullWindings(bh2);
  255. }
  256. {
  257. vec_t volume_brush_1;
  258. vec_t volume_brush_2;
  259. vec_t volume_brush_union;
  260. vec_t volume_ratio_1;
  261. vec_t volume_ratio_2;
  262. if (isInvalidHull(&union_hull))
  263. {
  264. FreeFaceList(union_hull.faces);
  265. continue;
  266. }
  267. volume_brush_union = CalculateSolidVolume(&union_hull);
  268. volume_brush_1 = CalculateSolidVolume(bh1);
  269. volume_brush_2 = CalculateSolidVolume(bh2);
  270. volume_ratio_1 = volume_brush_union / volume_brush_1;
  271. volume_ratio_2 = volume_brush_union / volume_brush_2;
  272. if ((volume_ratio_1 > g_BrushUnionThreshold) || (g_developer >= DEVELOPER_LEVEL_MESSAGE))
  273. {
  274. volume_ratio_1 *= 100.0;
  275. Warning("Entity %d : Brush %d intersects with brush %d by %2.3f percent", b1->entitynum,
  276. brushnum, bn, volume_ratio_1);
  277. }
  278. if ((volume_ratio_2 > g_BrushUnionThreshold) || (g_developer >= DEVELOPER_LEVEL_MESSAGE))
  279. {
  280. volume_ratio_2 *= 100.0;
  281. Warning("Entity %d : Brush %d intersects with brush %d by %2.3f percent", b1->entitynum, bn,
  282. brushnum, volume_ratio_2);
  283. }
  284. }
  285. FreeFaceList(union_hull.faces);
  286. }
  287. }
  288. }
  289. }