source: src/Shapes/BaseShapes.cpp@ 0eb8f4

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 Candidate_v1.7.0 Candidate_v1.7.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 0eb8f4 was 0eb8f4, checked in by Frederik Heber <heber@…>, 13 years ago

Cylinder implementation.

Taken from somehow broken branch, so rebase was not possible,
unfortunately.

The cylinder shape is centered on (0 0 0) and its height (z-Axis)
ranges from -1 to 1. Different scalings can be applied with the special
constructor.

  • Property mode set to 100644
File size: 10.1 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/RealSpaceMatrix.hpp"
32#include "LinearAlgebra/Line.hpp"
33#include "LinearAlgebra/Plane.hpp"
34#include "LinearAlgebra/LineSegment.hpp"
35#include "LinearAlgebra/LineSegmentSet.hpp"
36
37#include <cmath>
38#include <algorithm>
39
40// CYLINDER CODE
41// ----------------------------------------------------------------------------
42bool Cylinder_impl::isInside(const Vector &point) const {
43 return (Vector(point[0], point[1], 0.0).NormSquared() < 1.0+MYEPSILON) &&
44 (point[2] > -1.0-MYEPSILON) && (point[2] < 1.0+MYEPSILON);
45}
46
47bool Cylinder_impl::isOnSurface(const Vector &point) const {
48 return fabs(Vector(point[0], point[1], 0.0).NormSquared()-1.0)<MYEPSILON &&
49 (point[2] > -1.0-MYEPSILON) && (point[2] < 1.0+MYEPSILON);
50
51}
52
53Vector Cylinder_impl::getNormal(const Vector &point) const throw(NotOnSurfaceException) {
54 if(!isOnSurface(point)){
55 throw NotOnSurfaceException() << ShapeVector(&point);
56 }
57
58 if ((fabs(point[2]-1)<MYEPSILON) || (fabs(point[2])<MYEPSILON))
59 return Vector(0.0, 0.0, point[2]);
60 else
61 return Vector(point[0], point[1], 0.0);
62}
63
64Vector Cylinder_impl::getCenter() const
65{
66 return Vector(0.0, 0.0, 0.0);
67}
68
69double Cylinder_impl::getRadius() const
70{
71 return 1.0;
72}
73
74double Cylinder_impl::getVolume() const
75{
76 return M_PI*2.0; // pi r^2 h
77}
78
79double Cylinder_impl::getSurfaceArea() const
80{
81 return 2.0*M_PI*2.0; // 2 pi r h
82}
83
84LineSegmentSet Cylinder_impl::getLineIntersections(const Line &line) const {
85 // TODO
86 ASSERT(0, "Cylinder_impl::getLineIntersections - not implemented.");
87}
88
89std::string Cylinder_impl::toString() const
90{
91 return "Cylinder()";
92}
93
94enum ShapeType Cylinder_impl::getType() const
95{
96 return CylinderType;
97}
98
99std::vector<Vector> Cylinder_impl::getHomogeneousPointsOnSurface(const size_t N) const {
100 // TODO
101 ASSERT(0, "Cylinder_impl::getHomogeneousPointsOnSurface - not implemented.");
102}
103
104std::vector<Vector> Cylinder_impl::getHomogeneousPointsInVolume(const size_t N) const {
105 // TODO
106 ASSERT(0, "Cylinder_impl::getHomogeneousPointsInVolume - not implemented.");
107}
108
109Shape Cylinder() {
110 Shape::impl_ptr impl = Shape::impl_ptr(new Cylinder_impl());
111 return Shape(impl);
112}
113
114Shape Cylinder(const Vector &center, const double xrot, const double yrot,
115 const double height, const double radius)
116{
117 RealSpaceMatrix rot;
118 rot.setRotation(xrot, yrot, 0.0);
119
120 return translate(
121 transform(
122 stretch(
123 Cylinder(),
124 Vector(radius, radius, height*0.5)),
125 rot),
126 center);
127}
128// ----------------------------------------------------------------------------
129
130bool Sphere_impl::isInside(const Vector &point) const{
131 return point.NormSquared()<=1.;
132}
133
134bool Sphere_impl::isOnSurface(const Vector &point) const{
135 return fabs(point.NormSquared()-1.)<MYEPSILON;
136}
137
138Vector Sphere_impl::getNormal(const Vector &point) const throw(NotOnSurfaceException){
139 if(!isOnSurface(point)){
140 throw NotOnSurfaceException() << ShapeVector(&point);
141 }
142 return point;
143}
144
145Vector Sphere_impl::getCenter() const
146{
147 return Vector(0.,0.,0.);
148}
149
150double Sphere_impl::getRadius() const
151{
152 return 1.;
153}
154
155double Sphere_impl::getVolume() const
156{
157 return (4./3.)*M_PI; // 4/3 pi r^3
158}
159
160double Sphere_impl::getSurfaceArea() const
161{
162 return 2.*M_PI; // 2 pi r^2
163}
164
165
166LineSegmentSet Sphere_impl::getLineIntersections(const Line &line) const{
167 LineSegmentSet res(line);
168 std::vector<Vector> intersections = line.getSphereIntersections();
169 if(intersections.size()==2){
170 res.insert(LineSegment(intersections[0],intersections[1]));
171 }
172 return res;
173}
174
175std::string Sphere_impl::toString() const{
176 return "Sphere()";
177}
178
179enum ShapeType Sphere_impl::getType() const
180{
181 return SphereType;
182}
183
184/**
185 * algorithm taken from http://www.cgafaq.info/wiki/Evenly_distributed_points_on_sphere
186 * \param N number of points on surface
187 */
188std::vector<Vector> Sphere_impl::getHomogeneousPointsOnSurface(const size_t N) const
189{
190 std::vector<Vector> PointsOnSurface;
191 if (true) {
192 // Exactly N points but not symmetric.
193
194 // This formula is derived by finding a curve on the sphere that spirals down from
195 // the north pole to the south pole keeping a constant distance between consecutive turns.
196 // The curve is then parametrized by arch length and evaluated in constant intervals.
197 double a = sqrt(N) * 2;
198 for (int i=0; i<N; i++){
199 double t0 = ((double)i + 0.5) / (double)N;
200 double t = (sqrt(t0) - sqrt(1.0 - t0) + 1.0) / 2.0 * M_PI;
201 Vector point;
202 point.Zero();
203 point[0] = sin(t) * sin(t * a);
204 point[1] = sin(t) * cos(t * a);
205 point[2] = cos(t);
206 PointsOnSurface.push_back(point);
207 }
208 ASSERT(PointsOnSurface.size() == N,
209 "Sphere_impl::getHomogeneousPointsOnSurface() did not create "
210 +::toString(N)+" but "+::toString(PointsOnSurface.size())+" points.");
211 } else {
212 // Symmetric but only approximately N points.
213 double a=4*M_PI/N;
214 double d= sqrt(a);
215 int Mtheta=int(M_PI/d);
216 double dtheta=M_PI/Mtheta;
217 double dphi=a/dtheta;
218 for (int m=0; m<Mtheta; m++)
219 {
220 double theta=M_PI*(m+0.5)/Mtheta;
221 int Mphi=int(2*M_PI*sin(theta)/dphi);
222 for (int n=0; n<Mphi;n++)
223 {
224 double phi= 2*M_PI*n/Mphi;
225 Vector point;
226 point.Zero();
227 point[0]=sin(theta)*cos(phi);
228 point[1]=sin(theta)*sin(phi);
229 point[2]=cos(theta);
230 PointsOnSurface.push_back(point);
231 }
232 }
233 }
234 return PointsOnSurface;
235}
236
237std::vector<Vector> Sphere_impl::getHomogeneousPointsInVolume(const size_t N) const {
238 ASSERT(0,
239 "Sphere_impl::getHomogeneousPointsInVolume() - not implemented.");
240 return std::vector<Vector>();
241}
242
243Shape Sphere(){
244 Shape::impl_ptr impl = Shape::impl_ptr(new Sphere_impl());
245 return Shape(impl);
246}
247
248Shape Sphere(const Vector &center,double radius){
249 return translate(resize(Sphere(),radius),center);
250}
251
252Shape Ellipsoid(const Vector &center, const Vector &radius){
253 return translate(stretch(Sphere(),radius),center);
254}
255
256bool Cuboid_impl::isInside(const Vector &point) const{
257 return (point[0]>=0 && point[0]<=1) && (point[1]>=0 && point[1]<=1) && (point[2]>=0 && point[2]<=1);
258}
259
260bool Cuboid_impl::isOnSurface(const Vector &point) const{
261 bool retVal = isInside(point);
262 // test all borders of the cuboid
263 // double fabs
264 retVal = retVal &&
265 (((fabs(point[0]-1.) < MYEPSILON) || (fabs(point[0]) < MYEPSILON)) ||
266 ((fabs(point[1]-1.) < MYEPSILON) || (fabs(point[1]) < MYEPSILON)) ||
267 ((fabs(point[2]-1.) < MYEPSILON) || (fabs(point[2]) < MYEPSILON)));
268 return retVal;
269}
270
271Vector Cuboid_impl::getNormal(const Vector &point) const throw(NotOnSurfaceException){
272 if(!isOnSurface(point)){
273 throw NotOnSurfaceException() << ShapeVector(&point);
274 }
275 Vector res;
276 // figure out on which sides the Vector lies (maximum 3, when it is in a corner)
277 for(int i=NDIM;i--;){
278 if(fabs(fabs(point[i])-1)<MYEPSILON){
279 // add the scaled (-1/+1) Vector to the set of surface vectors
280 res[i] = point[i];
281 }
282 }
283 ASSERT(res.NormSquared()>=1 && res.NormSquared()<=3,"To many or to few sides found for this Vector");
284
285 res.Normalize();
286 return res;
287}
288
289
290Vector Cuboid_impl::getCenter() const
291{
292 return Vector(0.5,0.5,0.5);
293}
294
295double Cuboid_impl::getRadius() const
296{
297 return .5;
298}
299
300double Cuboid_impl::getVolume() const
301{
302 return 1.; // l^3
303}
304
305double Cuboid_impl::getSurfaceArea() const
306{
307 return 6.; // 6 * l^2
308}
309
310LineSegmentSet Cuboid_impl::getLineIntersections(const Line &line) const{
311 LineSegmentSet res(line);
312 // get the intersection on each of the six faces
313 std::vector<Vector> intersections;
314 intersections.resize(2);
315 int c=0;
316 int x[2]={-1,+1};
317 for(int i=NDIM;i--;){
318 for(int p=0;p<2;++p){
319 if(c==2) goto end; // I know this sucks, but breaking two loops is stupid
320 Vector base;
321 base[i]=x[p];
322 // base now points to the surface and is normal to it at the same time
323 Plane p(base,base);
324 Vector intersection = p.GetIntersection(line);
325 if(isInside(intersection)){
326 // if we have a point on the edge it might already be contained
327 if(c==1 && intersections[0]==intersection)
328 continue;
329 intersections[c++]=intersection;
330 }
331 }
332 }
333 end:
334 if(c==2){
335 res.insert(LineSegment(intersections[0],intersections[1]));
336 }
337 return res;
338}
339
340std::string Cuboid_impl::toString() const{
341 return "Cuboid()";
342}
343
344enum ShapeType Cuboid_impl::getType() const
345{
346 return CuboidType;
347}
348
349/**
350 * \param N number of points on surface
351 */
352std::vector<Vector> Cuboid_impl::getHomogeneousPointsOnSurface(const size_t N) const {
353 std::vector<Vector> PointsOnSurface;
354 ASSERT(false, "Cuboid_impl::getHomogeneousPointsOnSurface() not implemented yet");
355 return PointsOnSurface;
356}
357
358std::vector<Vector> Cuboid_impl::getHomogeneousPointsInVolume(const size_t N) const {
359 ASSERT(0,
360 "Cuboid_impl::getHomogeneousPointsInVolume() - not implemented.");
361 return std::vector<Vector>();
362}
363
364Shape Cuboid(){
365 Shape::impl_ptr impl = Shape::impl_ptr(new Cuboid_impl());
366 return Shape(impl);
367}
368
369Shape Cuboid(const Vector &corner1, const Vector &corner2){
370 // make sure the two edges are upper left front and lower right back
371 Vector sortedC1;
372 Vector sortedC2;
373 for(int i=NDIM;i--;){
374 sortedC1[i] = std::min(corner1[i],corner2[i]);
375 sortedC2[i] = std::max(corner1[i],corner2[i]);
376 ASSERT(corner1[i]!=corner2[i],"Given points for cuboid edges did not define a valid space");
377 }
378 // get the middle point
379 Vector middle = (1./2.)*(sortedC1+sortedC2);
380 Vector factors = sortedC2-middle;
381 return translate(stretch(Cuboid(),factors),middle);
382}
Note: See TracBrowser for help on using the repository browser.