source: src/Shapes/BaseShapes.cpp@ 571d04

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 571d04 was 735940, checked in by Frederik Heber <heber@…>, 13 years ago

FIX: Made practically all Shape... member functions const.

  • Shape: isInside(), isOnSurface(), getNormal(), getLineIntersections()
  • ShapeOps: translateIn(), translateOut(), translateOutNormal(), isInside().
  • Property mode set to 100644
File size: 6.4 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
8/*
9 * BaseShapes_impl.cpp
10 *
11 * Created on: Jun 18, 2010
12 * Author: crueger
13 */
14
15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20#include "CodePatterns/MemDebug.hpp"
21
22#include "Shapes/BaseShapes.hpp"
23#include "Shapes/BaseShapes_impl.hpp"
24#include "Shapes/ShapeExceptions.hpp"
25#include "Shapes/ShapeOps.hpp"
26
27#include "Helpers/defs.hpp"
28
29#include "CodePatterns/Assert.hpp"
30#include "LinearAlgebra/Vector.hpp"
31#include "LinearAlgebra/Line.hpp"
32#include "LinearAlgebra/Plane.hpp"
33#include "LinearAlgebra/LineSegment.hpp"
34#include "LinearAlgebra/LineSegmentSet.hpp"
35
36#include <cmath>
37#include <algorithm>
38
39bool Sphere_impl::isInside(const Vector &point) const{
40 return point.NormSquared()<=1.;
41}
42
43bool Sphere_impl::isOnSurface(const Vector &point) const{
44 return fabs(point.NormSquared()-1.)<MYEPSILON;
45}
46
47Vector Sphere_impl::getNormal(const Vector &point) const throw(NotOnSurfaceException){
48 if(!isOnSurface(point)){
49 throw NotOnSurfaceException() << ShapeVector(&point);
50 }
51 return point;
52}
53
54LineSegmentSet Sphere_impl::getLineIntersections(const Line &line) const{
55 LineSegmentSet res(line);
56 std::vector<Vector> intersections = line.getSphereIntersections();
57 if(intersections.size()==2){
58 res.insert(LineSegment(intersections[0],intersections[1]));
59 }
60 return res;
61}
62
63std::string Sphere_impl::toString() const{
64 return "Sphere()";
65}
66
67enum ShapeType Sphere_impl::getType() const
68{
69 return SphereType;
70}
71
72/**
73 * algorithm taken from http://www.cgafaq.info/wiki/Evenly_distributed_points_on_sphere
74 * \param N number of points on surface
75 */
76std::vector<Vector> Sphere_impl::getHomogeneousPointsOnSurface(const size_t N) const
77{
78 std::vector<Vector> PointsOnSurface;
79
80 double PI=3.14159265358979323846;
81 double a=4*PI/N;
82 double d= sqrt(a);
83 int Mtheta=int(PI/d);
84 double dtheta=PI/Mtheta;
85 double dphi=a/dtheta;
86 for (int m=0; m<Mtheta; m++)
87 {
88 double theta=PI*(m+0.5)/Mtheta;
89 int Mphi=int(2*PI*sin(theta)/dphi);
90 for (int n=0; n<Mphi;n++)
91 {
92 double phi= 2*PI*n/Mphi;
93 Vector point;
94 point.Zero();
95 point[0]=sin(theta)*cos(phi);
96 point[1]=sin(theta)*sin(phi);
97 point[2]=cos(theta);
98 PointsOnSurface.push_back(point);
99 }
100 }
101 /*const double dlength = M_PI*(3.-sqrt(5.));
102 double length = 0;
103 const double dz = 2.0/N;
104 double z = 1. - dz/2.;
105 Vector point;
106 for (size_t ka = 0; ka<N; ka++){
107 const double r = sqrt(1.-z*z);
108 point.Zero();
109 point[0] = cos(length)*r;
110 point[1] = sin(length)*r;
111 point[2] = z;
112 PointsOnSurface.push_back(point);
113 z = z - dz;
114 length = length + dlength;*/
115
116// ASSERT(PointsOnSurface.size() == N,
117// "Sphere_impl::getHomogeneousPointsOnSurface() did not create "
118// +::toString(N)+" but "+::toString(PointsOnSurface.size())+" points.");
119 return PointsOnSurface;
120}
121
122
123Shape Sphere(){
124 Shape::impl_ptr impl = Shape::impl_ptr(new Sphere_impl());
125 return Shape(impl);
126}
127
128Shape Sphere(const Vector &center,double radius){
129 return translate(resize(Sphere(),radius),center);
130}
131
132Shape Ellipsoid(const Vector &center, const Vector &radius){
133 return translate(stretch(Sphere(),radius),center);
134}
135
136bool Cuboid_impl::isInside(const Vector &point) const{
137 return (point[0]>=0 && point[0]<=1) && (point[1]>=0 && point[1]<=1) && (point[2]>=0 && point[2]<=1);
138}
139
140bool Cuboid_impl::isOnSurface(const Vector &point) const{
141 bool retVal = isInside(point);
142 // test all borders of the cuboid
143 // double fabs
144 retVal = retVal &&
145 (((fabs(point[0]-1.) < MYEPSILON) || (fabs(point[0]) < MYEPSILON)) ||
146 ((fabs(point[1]-1.) < MYEPSILON) || (fabs(point[1]) < MYEPSILON)) ||
147 ((fabs(point[2]-1.) < MYEPSILON) || (fabs(point[2]) < MYEPSILON)));
148 return retVal;
149}
150
151Vector Cuboid_impl::getNormal(const Vector &point) const throw(NotOnSurfaceException){
152 if(!isOnSurface(point)){
153 throw NotOnSurfaceException() << ShapeVector(&point);
154 }
155 Vector res;
156 // figure out on which sides the Vector lies (maximum 3, when it is in a corner)
157 for(int i=NDIM;i--;){
158 if(fabs(fabs(point[i])-1)<MYEPSILON){
159 // add the scaled (-1/+1) Vector to the set of surface vectors
160 res[i] = point[i];
161 }
162 }
163 ASSERT(res.NormSquared()>=1 && res.NormSquared()<=3,"To many or to few sides found for this Vector");
164
165 res.Normalize();
166 return res;
167}
168
169LineSegmentSet Cuboid_impl::getLineIntersections(const Line &line) const{
170 LineSegmentSet res(line);
171 // get the intersection on each of the six faces
172 std::vector<Vector> intersections;
173 intersections.resize(2);
174 int c=0;
175 int x[2]={-1,+1};
176 for(int i=NDIM;i--;){
177 for(int p=0;p<2;++p){
178 if(c==2) goto end; // I know this sucks, but breaking two loops is stupid
179 Vector base;
180 base[i]=x[p];
181 // base now points to the surface and is normal to it at the same time
182 Plane p(base,base);
183 Vector intersection = p.GetIntersection(line);
184 if(isInside(intersection)){
185 // if we have a point on the edge it might already be contained
186 if(c==1 && intersections[0]==intersection)
187 continue;
188 intersections[c++]=intersection;
189 }
190 }
191 }
192 end:
193 if(c==2){
194 res.insert(LineSegment(intersections[0],intersections[1]));
195 }
196 return res;
197}
198
199std::string Cuboid_impl::toString() const{
200 return "Cuboid()";
201}
202
203enum ShapeType Cuboid_impl::getType() const
204{
205 return CuboidType;
206}
207
208/**
209 * \param N number of points on surface
210 */
211std::vector<Vector> Cuboid_impl::getHomogeneousPointsOnSurface(const size_t N) const {
212 std::vector<Vector> PointsOnSurface;
213 ASSERT(false, "Cuboid_impl::getHomogeneousPointsOnSurface() not implemented yet");
214 return PointsOnSurface;
215}
216
217Shape Cuboid(){
218 Shape::impl_ptr impl = Shape::impl_ptr(new Cuboid_impl());
219 return Shape(impl);
220}
221
222Shape Cuboid(const Vector &corner1, const Vector &corner2){
223 // make sure the two edges are upper left front and lower right back
224 Vector sortedC1;
225 Vector sortedC2;
226 for(int i=NDIM;i--;){
227 sortedC1[i] = std::min(corner1[i],corner2[i]);
228 sortedC2[i] = std::max(corner1[i],corner2[i]);
229 ASSERT(corner1[i]!=corner2[i],"Given points for cuboid edges did not define a valid space");
230 }
231 // get the middle point
232 Vector middle = (1./2.)*(sortedC1+sortedC2);
233 Vector factors = sortedC2-middle;
234 return translate(stretch(Cuboid(),factors),middle);
235}
Note: See TracBrowser for help on using the repository browser.