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

Function vm_GetCentroidFast

scripts/osiris_vector.h:857–907  ·  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

855// Gets the real center of a polygon, but uses fast magnitude calculation
856// Returns the size of the passed in stuff
857float vm_GetCentroidFast(vector *centroid, vector *src, int nv) {
858 // ASSERT (nv>2);
859 vector normal;
860 float area, total_area;
861 int i;
862 vector tmp_center;
863
864 vm_MakeZero(centroid);
865
866 // First figure out the total area of this polygon
867 vm_GetPerp(&normal, &src[0], &src[1], &src[2]);
868 total_area = (vm_GetMagnitudeFast(&normal) / 2);
869
870 for (i = 2; i < nv - 1; i++) {
871 vm_GetPerp(&normal, &src[0], &src[i], &src[i + 1]);
872 area = (vm_GetMagnitudeFast(&normal) / 2);
873 total_area += area;
874 }
875
876 // Now figure out how much weight each triangle represents to the overall
877 // polygon
878 vm_GetPerp(&normal, &src[0], &src[1], &src[2]);
879 area = (vm_GetMagnitudeFast(&normal) / 2);
880
881 // Get the center of the first polygon
882 vm_MakeZero(&tmp_center);
883 for (i = 0; i < 3; i++) {
884 tmp_center += src[i];
885 }
886 tmp_center /= 3;
887
888 *centroid += (tmp_center * (area / total_area));
889
890 // Now do the same for the rest
891 for (i = 2; i < nv - 1; i++) {
892 vm_GetPerp(&normal, &src[0], &src[i], &src[i + 1]);
893 area = (vm_GetMagnitudeFast(&normal) / 2);
894
895 vm_MakeZero(&tmp_center);
896
897 tmp_center += src[0];
898 tmp_center += src[i];
899 tmp_center += src[i + 1];
900
901 tmp_center /= 3;
902
903 *centroid += (tmp_center * (area / total_area));
904 }
905
906 return total_area;
907}
908
909// creates a completely random, non-normalized vector with a range of values from -1023 to +1024 values)
910void vm_MakeRandomVector(vector *vec) {

Callers

nothing calls this directly

Calls 3

vm_MakeZeroFunction · 0.85
vm_GetPerpFunction · 0.70
vm_GetMagnitudeFastFunction · 0.70

Tested by

no test coverage detected