clippingPresets.cpp 7.66 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009-2011, IGG Team, LSIIT, University of Strasbourg           *
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library is distributed in the hope that it will be useful, but WITHOUT  *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
* Web site: http://cgogn.u-strasbg.fr/                                         *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Utils/clippingPresets.h"

namespace CGoGN
{

namespace Utils
{


/***********************************************
 *
 * 		Constructors
 *
 ***********************************************/


ClippingPreset* ClippingPreset::CreateEmptyPreset()
{
	ClippingPreset *preset = new ClippingPreset;

	return preset;
}

48
ClippingPreset* ClippingPreset::CreateDualPlanesPreset(Geom::Vec3f center, float size, int axis, bool facing)
49 50 51
{
	ClippingPreset *preset = new ClippingPreset;

52
	// Correct axis if necessary
53 54
	if ((axis < 0) || (axis > 2))
		axis = 0;
55 56

	// Axis on which planes will be aligned
57 58 59 60 61 62
	Geom::Vec3f positDir (0.0f, 0.0f, 0.0f);
	positDir[axis] = 1.0f;
	Geom::Vec3f negDir (0.0f, 0.0f, 0.0f);
	negDir[axis] = -1.0f;

	// Facing of planes
63
	float side = 1.0f;
64
	if (facing)
65
		side = -1.0f;
66 67

	// Add planes to preset
68 69
	preset->addClipPlane(positDir, center + positDir*(size / 2.0f)*(side));
	preset->addClipPlane(negDir, center + negDir*(size / 2.0f)*(side));
70 71

	// Set clipping mode
72 73 74 75
	ClippingShader::clippingMode clipMode = ClippingShader::CLIPPING_MODE_AND;
	if (facing)
		clipMode = ClippingShader::CLIPPING_MODE_OR;
	preset->setClippingMode(clipMode);
76 77 78 79

	return preset;
}

80
ClippingPreset* ClippingPreset::CreateCubePreset(Geom::Vec3f center, float size, bool facing)
81 82 83 84
{
	ClippingPreset *preset = new ClippingPreset;

	// Directions
85 86 87 88 89 90 91 92
	Geom::Vec3f xAxisPos = Geom::Vec3f (1.0f, 0.0f, 0.0f);
	Geom::Vec3f xAxisNeg = Geom::Vec3f (-1.0f, 0.0f, 0.0f);
	Geom::Vec3f yAxisPos = Geom::Vec3f (0.0f, 1.0f, 0.0f);
	Geom::Vec3f yAxisNeg = Geom::Vec3f (0.0f, -1.0f, 0.0f);
	Geom::Vec3f zAxisPos = Geom::Vec3f (0.0f, 0.0f, 1.0f);
	Geom::Vec3f zAxisNeg = Geom::Vec3f (0.0f, 0.0f, -1.0f);

	// Facing of planes
93
	float side = 1.0f;
94
	if (facing)
95
		side = -1.0f;
96 97

	// Add planes to preset
98 99 100 101 102 103
	preset->addClipPlane(xAxisPos, center + xAxisPos*(size / 2.0f)*(side));
	preset->addClipPlane(xAxisNeg, center + xAxisNeg*(size / 2.0f)*(side));
	preset->addClipPlane(yAxisPos, center + yAxisPos*(size / 2.0f)*(side));
	preset->addClipPlane(yAxisNeg, center + yAxisNeg*(size / 2.0f)*(side));
	preset->addClipPlane(zAxisPos, center + zAxisPos*(size / 2.0f)*(side));
	preset->addClipPlane(zAxisNeg, center + zAxisNeg*(size / 2.0f)*(side));
104 105

	// Set clipping mode
106 107 108 109
	ClippingShader::clippingMode clipMode = ClippingShader::CLIPPING_MODE_AND;
	if (facing)
		clipMode = ClippingShader::CLIPPING_MODE_OR;
	preset->setClippingMode(clipMode);
110 111 112 113

	return preset;
}

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
ClippingPreset* ClippingPreset::CreateTubePreset(Geom::Vec3f center, float size, int axis, int precision, bool facing)
{
	ClippingPreset *preset = new ClippingPreset;

	// Correct axis if necessary
	if ((axis < 0) || (axis > 2))
		axis = 0;

	// Correct precision if necessary
	if (precision < 1)
		precision = 1;

	// Facing of planes
	float side = 1.0f;
	if (facing)
		side = -1.0f;

	// Add planes to preset
132
	Geom::Vec3f direction (0.0f, 0.0f, 0.0f);
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
	float dAngle = 2.0f * M_PI / precision;
	for (int i = 0; i < precision; i++)
	{
		direction[(axis + 1) % 3] = cos(i*dAngle);
		direction[(axis + 2) % 3] = sin(i*dAngle);
		preset->addClipPlane(direction, center + direction*(size / 2.0f)*(side));
	}

	// Set clipping mode
	ClippingShader::clippingMode clipMode = ClippingShader::CLIPPING_MODE_AND;
	if (facing)
		clipMode = ClippingShader::CLIPPING_MODE_OR;
	preset->setClippingMode(clipMode);

	return preset;
}

ClippingPreset* ClippingPreset::CreateMoleculePreset(Geom::Vec3f center, float size, float atomsRadiuses, bool orClipping)
{
	ClippingPreset *preset = new ClippingPreset;

	// Add spheres to preset on each axis
	for (int i = 0; i < 3; i++)
	{
		Geom::Vec3f positDir (0.0f, 0.0f, 0.0f);
		positDir[i] = 1.0f;
		Geom::Vec3f negDir (0.0f, 0.0f, 0.0f);
		negDir[i] = -1.0f;
		preset->addClipSphere(center + positDir*(size / 2.0f), atomsRadiuses);
		preset->addClipSphere(center + negDir*(size / 2.0f), atomsRadiuses);
	}

	// Set clipping mode
	ClippingShader::clippingMode clipMode = ClippingShader::CLIPPING_MODE_AND;
	if (orClipping)
		clipMode = ClippingShader::CLIPPING_MODE_OR;
	preset->setClippingMode(clipMode);

	return preset;
}

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
ClippingPreset::ClippingPreset():
		m_clipMode (ClippingShader::CLIPPING_MODE_AND)
{

}


/***********************************************
 *
 * 		Preset settings
 *
 ***********************************************/


void ClippingPreset::addClipPlane(Geom::Vec3f normal, Geom::Vec3f origin)
{
	clipPlane newPlane;
	newPlane.normal = normal;
	newPlane.origin = origin;

	m_clipPlanes.push_back(newPlane);
}

void ClippingPreset::addClipSphere(Geom::Vec3f center, float radius)
{
	clipSphere newSphere;
	newSphere.center = center;
	newSphere.radius = radius;

	m_clipSpheres.push_back(newSphere);
}

void ClippingPreset::setClippingMode(ClippingShader::clippingMode clipMode)
{
	m_clipMode = clipMode;
}


/***********************************************
 *
 * 		Preset application
 *
 ***********************************************/


void ClippingPreset::apply(ClippingShader* clipShader, std::vector<unsigned int> *planesIds, std::vector<unsigned int> *spheresIds)
{
	if ( (clipShader == NULL) || (planesIds == NULL) || (spheresIds == NULL) )
		return;

	// Delete any remaining clip planes or spheres
	clipShader->deleteAllClipPlanes();
	clipShader->deleteAllClipSpheres();

	// Initialize ids arrays (they should already be of zero size)
	planesIds->resize(0);
	spheresIds->resize(0);

	// Add planes and spheres and get their ids
	for (size_t i = 0; i < m_clipPlanes.size(); i++)
	{
		unsigned int id = clipShader->addClipPlane();
		planesIds->push_back(id);
		clipShader->setClipPlaneParamsAll(id, m_clipPlanes[i].normal, m_clipPlanes[i].origin);
	}
	for (size_t i = 0; i < m_clipSpheres.size(); i++)
	{
		unsigned int id = clipShader->addClipSphere();
		spheresIds->push_back(id);
		clipShader->setClipSphereParamsAll(id, m_clipSpheres[i].center, m_clipSpheres[i].radius);
	}

	// Set new clipping mode
	clipShader->setClipMode(m_clipMode);

}

} // namespace Utils

} // namespace CGoGN