1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
5 | *
|
---|
6 | *
|
---|
7 | * This file is part of MoleCuilder.
|
---|
8 | *
|
---|
9 | * MoleCuilder is free software: you can redistribute it and/or modify
|
---|
10 | * it under the terms of the GNU General Public License as published by
|
---|
11 | * the Free Software Foundation, either version 2 of the License, or
|
---|
12 | * (at your option) any later version.
|
---|
13 | *
|
---|
14 | * MoleCuilder is distributed in the hope that it will be useful,
|
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
17 | * GNU General Public License for more details.
|
---|
18 | *
|
---|
19 | * You should have received a copy of the GNU General Public License
|
---|
20 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
---|
21 | */
|
---|
22 |
|
---|
23 | /*
|
---|
24 | * ShapeFactory.cpp
|
---|
25 | *
|
---|
26 | * Created on: Sep 5, 2012
|
---|
27 | * Author: ankele
|
---|
28 | */
|
---|
29 |
|
---|
30 | // include config.h
|
---|
31 | #ifdef HAVE_CONFIG_H
|
---|
32 | #include <config.h>
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | #include "CodePatterns/MemDebug.hpp"
|
---|
36 |
|
---|
37 | #include "ShapeFactory.hpp"
|
---|
38 |
|
---|
39 | #include "BaseShapes.hpp"
|
---|
40 | #include "ShapeOps.hpp"
|
---|
41 |
|
---|
42 | #include "CodePatterns/Singleton_impl.hpp"
|
---|
43 | #include "LinearAlgebra/Vector.hpp"
|
---|
44 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
|
---|
45 |
|
---|
46 | const char *ShapeFactory::shapeNames[MAX_ShapeType] = {"nowhere", "everywhere", "sphere", "cube", "polygon", "combined", "cylinder"};
|
---|
47 |
|
---|
48 | ShapeFactory::ShapeFactory()
|
---|
49 | {
|
---|
50 | // Create map (type -> name).
|
---|
51 | for (int i=0; i<MAX_ShapeType; i++)
|
---|
52 | shapeNameMap.insert(std::pair<ShapeType, std::string>((ShapeType)i, shapeNames[i]));
|
---|
53 |
|
---|
54 | // Create inverse map.
|
---|
55 | for (int i=0; i<MAX_ShapeType; i++)
|
---|
56 | nameShapeMap.insert(std::pair<std::string, ShapeType>(shapeNames[i], (ShapeType)i));
|
---|
57 | }
|
---|
58 |
|
---|
59 | ShapeType ShapeFactory::getShapeByName(const std::string& name)
|
---|
60 | {
|
---|
61 | NameShapeMap::iterator iter = nameShapeMap.find(name);
|
---|
62 | ASSERT(iter != nameShapeMap.end(),
|
---|
63 | "ShapeFactory::getShapeByName() - unknown name: "+name+".");
|
---|
64 | return iter->second;
|
---|
65 | }
|
---|
66 |
|
---|
67 | std::string ShapeFactory::getShapeName(ShapeType type)
|
---|
68 | {
|
---|
69 | ShapeNameMap::iterator iter = shapeNameMap.find(type);
|
---|
70 | ASSERT(iter != shapeNameMap.end(),
|
---|
71 | "ShapeFactory::getShapeName() - unknown type: "+toString(type)+".");
|
---|
72 | return iter->second;
|
---|
73 | }
|
---|
74 |
|
---|
75 | bool ShapeFactory::isValidShapeName(const std::string& name)
|
---|
76 | {
|
---|
77 | NameShapeMap::iterator iter = nameShapeMap.find(name);
|
---|
78 | return (iter != nameShapeMap.end());
|
---|
79 | }
|
---|
80 |
|
---|
81 | ShapeFactory::~ShapeFactory()
|
---|
82 | {
|
---|
83 | }
|
---|
84 |
|
---|
85 | Shape ShapeFactory::produce(ShapeType type, const Vector &translation, const Vector &stretch, double angleX, double angleY, double angleZ) const
|
---|
86 | {
|
---|
87 | for (int i=0; i<NDIM; i++){
|
---|
88 | ASSERT(stretch[i] > 0,
|
---|
89 | "ShapeFactory::setStretch() - non positive component.");
|
---|
90 | }
|
---|
91 |
|
---|
92 | // Create the basic shape.
|
---|
93 | Shape s = Nowhere();
|
---|
94 | if (type == NowhereType){
|
---|
95 | s = Nowhere();
|
---|
96 | }else if (type == EverywhereType){
|
---|
97 | s = Everywhere();
|
---|
98 | }else if (type == CuboidType){
|
---|
99 | s = Cuboid();
|
---|
100 | }else if (type == SphereType){
|
---|
101 | s = Sphere();
|
---|
102 | }else if (type == CylinderType){
|
---|
103 | s = Cylinder();
|
---|
104 | }else{
|
---|
105 | ASSERT(false,
|
---|
106 | "ShapeFactory::produce - unhandled shape type: "+toString(type)+".");
|
---|
107 | }
|
---|
108 |
|
---|
109 | // Transform (if necessary).
|
---|
110 | if (stretch != Vector(1., 1., 1.))
|
---|
111 | s = ::stretch(s, stretch);
|
---|
112 | if ((angleX != 0) && (angleY != 0) && (angleZ != 0)){
|
---|
113 | RealSpaceMatrix rotation;
|
---|
114 | rotation.setRotation(angleX, angleY, angleZ);
|
---|
115 | s = transform(s, rotation);
|
---|
116 | }
|
---|
117 | if (!translation.IsZero())
|
---|
118 | s = translate(s, translation);
|
---|
119 |
|
---|
120 | return s;
|
---|
121 | }
|
---|
122 |
|
---|
123 | CONSTRUCT_SINGLETON(ShapeFactory)
|
---|