[bebafd] | 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 |
|
---|
[4477b7] | 39 | #include "BaseShapes.hpp"
|
---|
| 40 | #include "ShapeOps.hpp"
|
---|
| 41 |
|
---|
[bebafd] | 42 | #include "CodePatterns/Singleton_impl.hpp"
|
---|
[4477b7] | 43 | #include "LinearAlgebra/Vector.hpp"
|
---|
| 44 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
|
---|
[bebafd] | 45 |
|
---|
[1c9588] | 46 | const char *ShapeFactory::shapeNames[MAX_ShapeType] = {"nowhere", "everywhere", "sphere", "cube", "polygon", "combined", "cylinder"};
|
---|
[6e5d35] | 47 |
|
---|
[bebafd] | 48 | ShapeFactory::ShapeFactory()
|
---|
| 49 | {
|
---|
[6e5d35] | 50 | // Create map (type -> name).
|
---|
[1c9588] | 51 | for (int i=0; i<MAX_ShapeType; i++)
|
---|
[6e5d35] | 52 | shapeNameMap.insert(std::pair<ShapeType, std::string>((ShapeType)i, shapeNames[i]));
|
---|
| 53 |
|
---|
| 54 | // Create inverse map.
|
---|
[1c9588] | 55 | for (int i=0; i<MAX_ShapeType; i++)
|
---|
[6e5d35] | 56 | nameShapeMap.insert(std::pair<std::string, ShapeType>(shapeNames[i], (ShapeType)i));
|
---|
[dc438c] | 57 |
|
---|
| 58 | // Create baseShapeName list.
|
---|
| 59 | for (int i=0; i<MAX_ShapeType; i++)
|
---|
| 60 | if (strcmp(shapeNames[i], "combined") != 0)
|
---|
| 61 | baseShapeNames.push_back(shapeNames[i]);
|
---|
[6e5d35] | 62 | }
|
---|
| 63 |
|
---|
[1c9588] | 64 | ShapeType ShapeFactory::getShapeByName(const std::string& name)
|
---|
[6e5d35] | 65 | {
|
---|
| 66 | NameShapeMap::iterator iter = nameShapeMap.find(name);
|
---|
| 67 | ASSERT(iter != nameShapeMap.end(),
|
---|
| 68 | "ShapeFactory::getShapeByName() - unknown name: "+name+".");
|
---|
| 69 | return iter->second;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | std::string ShapeFactory::getShapeName(ShapeType type)
|
---|
| 73 | {
|
---|
| 74 | ShapeNameMap::iterator iter = shapeNameMap.find(type);
|
---|
| 75 | ASSERT(iter != shapeNameMap.end(),
|
---|
| 76 | "ShapeFactory::getShapeName() - unknown type: "+toString(type)+".");
|
---|
| 77 | return iter->second;
|
---|
[bebafd] | 78 | }
|
---|
| 79 |
|
---|
[98d03b] | 80 | bool ShapeFactory::isValidShapeName(const std::string& name)
|
---|
| 81 | {
|
---|
| 82 | NameShapeMap::iterator iter = nameShapeMap.find(name);
|
---|
| 83 | return (iter != nameShapeMap.end());
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[dc438c] | 86 | const std::vector<std::string> &ShapeFactory::getBaseShapeNames()
|
---|
| 87 | {
|
---|
| 88 | return baseShapeNames;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[bebafd] | 91 | ShapeFactory::~ShapeFactory()
|
---|
| 92 | {
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[842aac] | 95 | Shape ShapeFactory::produce(ShapeType type, const Vector &translation, const Vector &stretch, double angleX, double angleY, double angleZ) const
|
---|
[4f041a4] | 96 | {
|
---|
[a367b8] | 97 | for (int i=0; i<NDIM; i++){
|
---|
| 98 | ASSERT(stretch[i] > 0,
|
---|
[842aac] | 99 | "ShapeFactory::setStretch() - non positive component.");
|
---|
[a367b8] | 100 | }
|
---|
[4f041a4] | 101 |
|
---|
[4477b7] | 102 | // Create the basic shape.
|
---|
| 103 | Shape s = Nowhere();
|
---|
[1c9588] | 104 | if (type == NowhereType){
|
---|
[4477b7] | 105 | s = Nowhere();
|
---|
[1c9588] | 106 | }else if (type == EverywhereType){
|
---|
[4477b7] | 107 | s = Everywhere();
|
---|
[1c9588] | 108 | }else if (type == CuboidType){
|
---|
[4477b7] | 109 | s = Cuboid();
|
---|
[1c9588] | 110 | }else if (type == SphereType){
|
---|
[4477b7] | 111 | s = Sphere();
|
---|
[1c9588] | 112 | }else if (type == CylinderType){
|
---|
[4477b7] | 113 | s = Cylinder();
|
---|
| 114 | }else{
|
---|
| 115 | ASSERT(false,
|
---|
| 116 | "ShapeFactory::produce - unhandled shape type: "+toString(type)+".");
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | // Transform (if necessary).
|
---|
| 120 | if (stretch != Vector(1., 1., 1.))
|
---|
| 121 | s = ::stretch(s, stretch);
|
---|
[842aac] | 122 | if ((angleX != 0) && (angleY != 0) && (angleZ != 0)){
|
---|
| 123 | RealSpaceMatrix rotation;
|
---|
| 124 | rotation.setRotation(angleX, angleY, angleZ);
|
---|
| 125 | s = transform(s, rotation);
|
---|
| 126 | }
|
---|
[4477b7] | 127 | if (!translation.IsZero())
|
---|
| 128 | s = translate(s, translation);
|
---|
| 129 |
|
---|
| 130 | return s;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[bebafd] | 133 | CONSTRUCT_SINGLETON(ShapeFactory)
|
---|