geom_utils.cc

00001 /*
00002  *  This file is part of libcxxsupport.
00003  *
00004  *  libcxxsupport is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  libcxxsupport is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with libcxxsupport; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00017  */
00018 
00019 /*
00020  *  libcxxsupport is being developed at the Max-Planck-Institut fuer Astrophysik
00021  *  and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
00022  *  (DLR).
00023  */
00024 
00025 /*
00026  *  Copyright (C) 2011-2014 Max-Planck-Society
00027  *  \author Martin Reinecke
00028  */
00029 
00030 #include "geom_utils.h"
00031 #include "arr.h"
00032 
00033 using namespace std;
00034 
00035 namespace {
00036 
00037 void get_circle (const arr<vec3> &point, tsize q1, tsize q2, vec3 &center,
00038   double &cosrad)
00039   {
00040   center = (point[q1]+point[q2]).Norm();
00041   cosrad = dotprod(point[q1],center);
00042   for (tsize i=0; i<q1; ++i)
00043     if (dotprod(point[i],center)<cosrad) // point outside the current circle
00044       {
00045       center=crossprod(point[q1]-point[i],point[q2]-point[i]).Norm();
00046       cosrad=dotprod(point[i],center);
00047       if (cosrad<0)
00048         { center.Flip(); cosrad=-cosrad; }
00049       }
00050   }
00051 void get_circle (const arr<vec3> &point, tsize q, vec3 &center,
00052   double &cosrad)
00053   {
00054   center = (point[0]+point[q]).Norm();
00055   cosrad = dotprod(point[0],center);
00056   for (tsize i=1; i<q; ++i)
00057     if (dotprod(point[i],center)<cosrad) // point outside the current circle
00058       get_circle(point,i,q,center,cosrad);
00059   }
00060 
00061 } // unnamed namespace
00062 
00063 void find_enclosing_circle (const arr<vec3> &point, vec3 &center,
00064   double &cosrad)
00065   {
00066   tsize np=point.size();
00067   planck_assert(np>=2,"too few points");
00068   center = (point[0]+point[1]).Norm();
00069   cosrad = dotprod(point[0],center);
00070   for (tsize i=2; i<np; ++i)
00071     if (dotprod(point[i],center)<cosrad) // point outside the current circle
00072       get_circle(point,i,center,cosrad);
00073   }

Generated on Thu Oct 8 14:48:51 2015 for LevelS C++ support library