00001 #ifndef NMWR_GB_ALGEBRAIC_PRIMITIVES_C
00002 #define NMWR_GB_ALGEBRAIC_PRIMITIVES_C
00003
00004
00005
00006
00007
00008 #include "Geometry/algebraic-primitives.h"
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 template<class PIt, class Q>
00031 void get_polygon2d_center_and_area(PIt begin, PIt end,
00032 Q& center,
00033 double& area)
00034 {
00035 typedef point_traits<Q> Qpt;
00036
00037 typedef typename PIt::value_type P;
00038 typedef point_traits<P> Ppt;
00039
00040 REQUIRE((begin != end), "empty polygon!\n",1);
00041 PIt v1 = begin;
00042 PIt vi = v1;
00043 ++begin;
00044 REQUIRE((begin != end), "polygon with only one corner!\n",1);
00045 PIt vi1 = begin;
00046 double area_2 = 0.0, xtmp = 0.0, ytmp = 0.0;
00047
00048
00049
00050
00051
00052
00053 while( vi1 != end) {
00054 P pi=*vi,pi1=*vi1;
00055 double ai = Ppt::x(pi) * Ppt::y(pi1) - Ppt::y(pi) * Ppt::x(pi1);
00056 area_2 += ai;
00057 xtmp += ai * (Ppt::x(pi) + Ppt::x(pi1));
00058 ytmp += ai * (Ppt::y(pi) + Ppt::y(pi1));
00059 ++vi; ++vi1;
00060 }
00061
00062 vi1 = v1;
00063 P pi=*vi,pi1=*vi1;
00064 double ai = Ppt::x(pi) * Ppt::y(pi1) - Ppt::y(pi) * Ppt::x(pi1);
00065 area_2 += ai;
00066 xtmp += ai * (Ppt::x(pi) + Ppt::x(pi1));
00067 ytmp += ai * (Ppt::y(pi) + Ppt::y(pi1));
00068
00069
00070 area = 0.5 * area_2;
00071
00072 Qpt::x(center) = xtmp / (3.0 * area_2);
00073 Qpt::y(center) = ytmp / (3.0 * area_2);
00074 }
00075
00076 #endif