MCPcopy Create free account
hub / github.com/DescentDevelopers/Descent3 / vm_GetCentroidFast

Function vm_GetCentroidFast

vecmat/vector.cpp:807–857  ·  view source on GitHub ↗

Gets the real center of a polygon, but uses fast magnitude calculation Returns the size of the passed in stuff

Source from the content-addressed store, hash-verified

805// Gets the real center of a polygon, but uses fast magnitude calculation
806// Returns the size of the passed in stuff
807float vm_GetCentroidFast(vector *centroid, vector *src, int nv) {
808 ASSERT(nv > 2);
809 vector normal;
810 float area, total_area;
811 int i;
812 vector tmp_center;
813
814 vm_MakeZero(centroid);
815
816 // First figure out the total area of this polygon
817 vm_GetPerp(&normal, &src[0], &src[1], &src[2]);
818 total_area = (vm_GetMagnitudeFast(&normal) / 2);
819
820 for (i = 2; i < nv - 1; i++) {
821 vm_GetPerp(&normal, &src[0], &src[i], &src[i + 1]);
822 area = (vm_GetMagnitudeFast(&normal) / 2);
823 total_area += area;
824 }
825
826 // Now figure out how much weight each triangle represents to the overall
827 // polygon
828 vm_GetPerp(&normal, &src[0], &src[1], &src[2]);
829 area = (vm_GetMagnitudeFast(&normal) / 2);
830
831 // Get the center of the first polygon
832 vm_MakeZero(&tmp_center);
833 for (i = 0; i < 3; i++) {
834 tmp_center += src[i];
835 }
836 tmp_center /= 3;
837
838 *centroid += (tmp_center * (area / total_area));
839
840 // Now do the same for the rest
841 for (i = 2; i < nv - 1; i++) {
842 vm_GetPerp(&normal, &src[0], &src[i], &src[i + 1]);
843 area = (vm_GetMagnitudeFast(&normal) / 2);
844
845 vm_MakeZero(&tmp_center);
846
847 tmp_center += src[0];
848 tmp_center += src[i];
849 tmp_center += src[i + 1];
850
851 tmp_center /= 3;
852
853 *centroid += (tmp_center * (area / total_area));
854 }
855
856 return total_area;
857}
858
859// creates a completely random, non-normalized vector with a range of values from -1023 to +1024 values)
860void vm_MakeRandomVector(vector *vec) {

Callers 1

ClassifyAMFacesFunction · 0.50

Calls 3

vm_MakeZeroFunction · 0.85
vm_GetPerpFunction · 0.70
vm_GetMagnitudeFastFunction · 0.70

Tested by

no test coverage detected