clippingPresetsAnimated.cpp 11.4 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
/*******************************************************************************
* 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/clippingPresetsAnimated.h"
26 27
#define _USE_MATH_DEFINES
#include <math.h>
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

namespace CGoGN
{

namespace Utils
{


/***********************************************
 *
 * 		Base Class
 *
 ***********************************************/


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

	// Apply preset
	ClippingPreset::apply(clipShader, planesIds, spheresIds);

	// Store the clipping shader pointer
	m_attachedClippingShader = clipShader;

	// Store ids
	m_planesIds.resize(0);
	m_planesIds.insert(m_planesIds.begin(), planesIds->begin(), planesIds->end());
	m_spheresIds.resize(0);
	m_spheresIds.insert(m_spheresIds.begin(), spheresIds->begin(), spheresIds->end());
}


/***********************************************
 *
 * 		Derived Classes
 *
 ***********************************************/


ClippingPresetAnimatedDualPlanes::ClippingPresetAnimatedDualPlanes(
70
		Geom::Vec3f centerStart, Geom::Vec3f centerEnd, float size, int axis, bool facing, bool zigzag)
71
{
72
	// Store animation settings
73 74 75 76
	m_dirVec = centerEnd - centerStart;
	m_dirVec.normalize();
	m_startPoint = centerStart;
	m_endPoint = centerEnd;
77
	m_zigzag = zigzag;
78 79 80
	int usedAxis = axis;
	if ((usedAxis < 0) || (usedAxis > 2))
		usedAxis = 0;
81 82 83

	// Axis on which planes will be aligned
	Geom::Vec3f positDir (0.0f, 0.0f, 0.0f);
84
	positDir[usedAxis] = 1.0f;
85
	Geom::Vec3f negDir (0.0f, 0.0f, 0.0f);
86
	negDir[usedAxis] = -1.0f;
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

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

	// Add planes to preset
	addClipPlane(positDir, centerStart + positDir*(size / 2.0f)*(side));
	addClipPlane(negDir, centerStart + negDir*(size / 2.0f)*(side));

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

void ClippingPresetAnimatedDualPlanes::step(unsigned int amount)
{
	// Check if the animation has been stopped
	if (m_animationSpeedFactor == 0.0f)
		return;

	// Check the validity of planes or spheres ids
	if ( !m_attachedClippingShader->isClipPlaneIdValid(m_planesIds[0])
		|| !m_attachedClippingShader->isClipPlaneIdValid(m_planesIds[1]) )
	{
		CGoGNerr
		<< "ERROR -"
		<< "ClippingPresetAnimatedDualPlanes::step"
117
		<< " - Some planes or spheres ids are not valid anymore - Animation paused"
118 119 120 121 122 123 124 125 126 127
		<< CGoGNendl;
		m_animationSpeedFactor = 0.0f;
		return;
	}

	// Store the old center
	Geom::Vec3f oldCenter = (1.0f - m_animParam)*m_startPoint + m_animParam*m_endPoint;

	// Update animation parameter value
	m_animParam += (float)amount * m_animationOneStepIncrement * m_animationSpeedFactor;
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
	if (!m_zigzag)
	{
		while (m_animParam < 0.0f)
			m_animParam += 1.0f;
		while (m_animParam > 1.0f)
			m_animParam -= 1.0f;
	}
	else
	{
		while ( (m_animParam < 0.0f) || (m_animParam > 1.0f) )
		{
			if (m_animParam < 0.0f)
			{
				m_animParam = -m_animParam;
				m_animationOneStepIncrement = -m_animationOneStepIncrement;
			}
			else if (m_animParam > 1.0f)
			{
				m_animParam = 1.0f - (m_animParam - 1.0f);
				m_animationOneStepIncrement = -m_animationOneStepIncrement;
			}
		}
	}
151 152 153 154 155 156 157 158 159 160 161

	// Calculate new center
	Geom::Vec3f newCenter = (1.0f - m_animParam)*m_startPoint + m_animParam*m_endPoint;

	// Update clipping planes
	Geom::Vec3f plane1CurrPos = m_attachedClippingShader->getClipPlaneParamsOrigin(m_planesIds[0]);
	Geom::Vec3f plane2CurrPos = m_attachedClippingShader->getClipPlaneParamsOrigin(m_planesIds[1]);
	m_attachedClippingShader->setClipPlaneParamsOrigin(m_planesIds[0], plane1CurrPos + newCenter - oldCenter);
	m_attachedClippingShader->setClipPlaneParamsOrigin(m_planesIds[1], plane2CurrPos + newCenter - oldCenter);
}

Maire Nicolas's avatar
Maire Nicolas committed
162 163 164

ClippingPresetAnimatedRotatingPlane::ClippingPresetAnimatedRotatingPlane(Geom::Vec3f center, int axis)
{
165
	// Store animation settings
Maire Nicolas's avatar
Maire Nicolas committed
166
	m_axis = axis;
167 168
	if ((m_axis < 0) || (m_axis))
		m_axis = 0;
Maire Nicolas's avatar
Maire Nicolas committed
169 170 171

	// Axis on which planes will be aligned
	Geom::Vec3f normal (0.0f, 0.0f, 0.0f);
172
	normal[(m_axis + 1) % 3] = 1.0f;
Maire Nicolas's avatar
Maire Nicolas committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191

	// Add plane to preset
	addClipPlane(normal, center);

	// Set clipping mode
	setClippingMode(ClippingShader::CLIPPING_MODE_AND);
}

void ClippingPresetAnimatedRotatingPlane::step(unsigned int amount)
{
	// Check if the animation has been stopped
	if (m_animationSpeedFactor == 0.0f)
		return;

	// Check the validity of planes or spheres ids
	if (!m_attachedClippingShader->isClipPlaneIdValid(m_planesIds[0]))
	{
		CGoGNerr
		<< "ERROR -"
192 193
		<< "ClippingPresetAnimatedRotatingPlane::step"
		<< " - Some planes or spheres ids are not valid anymore - Animation paused"
Maire Nicolas's avatar
Maire Nicolas committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
		<< CGoGNendl;
		m_animationSpeedFactor = 0.0f;
		return;
	}

	// Update animation parameter value
	m_animParam += (float)amount * m_animationOneStepIncrement * m_animationSpeedFactor;
	while (m_animParam < 0.0f)
		m_animParam += 1.0f;
	while (m_animParam > 1.0f)
		m_animParam -= 1.0f;

	// Calculate new normal
	Geom::Vec3f planeNormal = m_attachedClippingShader->getClipPlaneParamsNormal(m_planesIds[0]);
	float angle = m_animParam*2.0*M_PI;
	planeNormal[(m_axis + 1) % 3] = cos(angle);
	planeNormal[(m_axis + 2) % 3] = sin(angle);
	m_attachedClippingShader->setClipPlaneParamsNormal(m_planesIds[0], planeNormal);
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
}

ClippingPresetAnimatedScaledSphere::ClippingPresetAnimatedScaledSphere(Geom::Vec3f center, float radiusStart, float radiusEnd, bool zigzag)
{
	// Store animation settings
	m_startRadius = radiusStart;
	m_endRadius = radiusEnd;
	std::cout << "Given Start Radius : " << radiusStart << std::endl;
	std::cout << "Actual Start Radius : " << m_startRadius << std::endl;
	std::cout << "Given End Radius : " << radiusEnd << std::endl;
	std::cout << "Actual End Radius : " << m_endRadius << std::endl;
	m_zigzag = zigzag;

	// Add sphere to preset
	addClipSphere(center, m_startRadius);

	// Set clipping mode
	setClippingMode(ClippingShader::CLIPPING_MODE_AND);
}
Maire Nicolas's avatar
Maire Nicolas committed
231

232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
void ClippingPresetAnimatedScaledSphere::step(unsigned int amount)
{
	// Check if the animation has been stopped
	if (m_animationSpeedFactor == 0.0f)
		return;

	// Check the validity of planes or spheres ids
	if (!m_attachedClippingShader->isClipSphereIdValid(m_spheresIds[0]))
	{
		CGoGNerr
		<< "ERROR -"
		<< "ClippingPresetAnimatedScaledSphere::step"
		<< " - Some planes or spheres ids are not valid anymore - Animation paused"
		<< CGoGNendl;
		m_animationSpeedFactor = 0.0f;
		return;
	}
Maire Nicolas's avatar
Maire Nicolas committed
249

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
	// Update animation parameter value
	m_animParam += (float)amount * m_animationOneStepIncrement * m_animationSpeedFactor;
	if (!m_zigzag)
	{
		while (m_animParam < 0.0f)
			m_animParam += 1.0f;
		while (m_animParam > 1.0f)
			m_animParam -= 1.0f;
	}
	else
	{
		while ( (m_animParam < 0.0f) || (m_animParam > 1.0f) )
		{
			if (m_animParam < 0.0f)
			{
				m_animParam = -m_animParam;
				m_animationOneStepIncrement = -m_animationOneStepIncrement;
			}
			else if (m_animParam > 1.0f)
			{
				m_animParam = 1.0f - (m_animParam - 1.0f);
				m_animationOneStepIncrement = -m_animationOneStepIncrement;
			}
		}
	}

	// Calculate new radius
	float radius = (1.0f - m_animParam)*m_startRadius + m_animParam*m_endRadius;
	m_attachedClippingShader->setClipSphereParamsRadius(m_spheresIds[0], radius);
}

ClippingPresetAnimatedSpheresCubeCollision::ClippingPresetAnimatedSpheresCubeCollision(Geom::Vec3f center, float size, int spheresCount, float radius)
{
	// Store animation settings
	m_cubeCenter = center;
	m_cubeSize = size;
	int usedSpheresCount = spheresCount;
	if (usedSpheresCount < 1)
		usedSpheresCount = 1;

	// Add spheres to preset
	for (int i = 0; i < usedSpheresCount; i++)
		addClipSphere(m_cubeCenter, radius);

	// Store spheres random directions
	m_spheresDirections.resize(usedSpheresCount);
	srand(time(NULL));
	for (size_t i = 0; i < m_spheresDirections.size(); i++)
	{
		Geom::Vec3f dir ((rand() % 1000) - 500, (rand() % 1000) - 500, (rand() % 1000) - 500);
		dir.normalize();
		m_spheresDirections[i] = dir;
	}

	// Set clipping mode
	setClippingMode(ClippingShader::CLIPPING_MODE_AND);
}

void ClippingPresetAnimatedSpheresCubeCollision::step(unsigned int amount)
{
	// Check if the animation has been stopped
	if (m_animationSpeedFactor == 0.0f)
		return;

	// Check the validity of planes or spheres ids
	for (size_t i = 0; i < m_spheresIds.size(); i++)
	{
		if (!m_attachedClippingShader->isClipSphereIdValid(m_spheresIds[i]))
		{
			CGoGNerr
			<< "ERROR -"
			<< "ClippingPresetAnimatedSpheresCubeCollision::step"
			<< " - Some planes or spheres ids are not valid anymore - Animation paused"
			<< CGoGNendl;
			m_animationSpeedFactor = 0.0f;
			return;
		}
	}

	// Update animation parameter value
	float dParam = (float)amount * m_animationOneStepIncrement * m_animationSpeedFactor;
	m_animParam += dParam;
	while (m_animParam < 0.0f)
		m_animParam += 1.0f;
	while (m_animParam > 1.0f)
		m_animParam -= 1.0f;

	// Calculate new center and detect collisions with cube faces
	for (size_t i = 0; i < m_spheresIds.size(); i++)
	{
		Geom::Vec3f oldCenter = m_attachedClippingShader->getClipSphereParamsCenter(m_spheresIds[i]);
		Geom::Vec3f newCenter = oldCenter + dParam*m_spheresDirections[i];
		m_attachedClippingShader->setClipSphereParamsCenter(m_spheresIds[i], newCenter);
		Geom::Vec3f posToCube = newCenter - m_cubeCenter;
		for (int j = 0; j < 3; j++)
		{
			if ( (posToCube[j] < -m_cubeSize*0.5f) || (posToCube[j] > m_cubeSize*0.5f) )
			{
				Geom::Vec3f cubeNormal (0.0f, 0.0f, 0.0f);
				if (posToCube[j] < 0.0f)
					cubeNormal[j] = 1.0f;
				else
					cubeNormal[j] = -1.0f;
				// Reflect
				if ( (m_spheresDirections[i] * cubeNormal) < 0.0f)
					m_spheresDirections[i] = (2.0f * ((cubeNormal*m_spheresDirections[i])*cubeNormal) - m_spheresDirections[i])*-1.0f;
			}
		}
	}
Maire Nicolas's avatar
Maire Nicolas committed
359 360
}

361 362 363
} // namespace Utils

} // namespace CGoGN