tuto_orbits.cpp 9.33 KB
Newer Older
1 2 3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
*                                                                              *
* 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.           *
*                                                                              *
20
* Web site: http://cgogn.unistra.fr/                                           *
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/


#include "tuto_orbits.h"
#include <iostream>


#include "Algo/Modelisation/primitives3d.h"
#include "Algo/Modelisation/polyhedron.h"
#include "Algo/Modelisation/subdivision.h"

#include "Algo/Render/GL2/topo3Render.h"
#include "Algo/Render/SVG/mapSVGRender.h"

#include "Algo/Import/import.h"
38
#include "Topology/generic/traversorFactory.h"
39

Pierre Kraemer's avatar
Pierre Kraemer committed
40
MAP myMap;
41
VertexAttribute<VEC3> position ;
42
DartAttribute<VEC3> middleDarts;
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60


void MyQT::text_onoff(bool x)
{
	render_text = !render_text;
	updateGL();
	CGoGNerr << " text_onoff  " << CGoGNflush;
}

void MyQT::slider_text(int x)
{
	m_strings->setScale(0.02f*x);
	updateGL();
}

void MyQT::orbit_list(int x)
{
	current_orbit = x;
61 62
	unsigned int orbs[9] = {VERTEX,EDGE,FACE,VOLUME,PFP::MAP::ORBIT_IN_PARENT(VERTEX),PFP::MAP::ORBIT_IN_PARENT(EDGE),PFP::MAP::ORBIT_IN_PARENT(FACE),PFP::MAP::ORBIT_IN_PARENT2(VERTEX),PFP::MAP::ORBIT_IN_PARENT2(EDGE)};
	storeVerticesInfoGen(orbs[current_orbit], m_att_orbits[x]);
Sylvain Thery's avatar
Sylvain Thery committed
63 64 65

	if (m_clicked != Dart::nil())
	{
66

Sylvain Thery's avatar
Sylvain Thery committed
67 68 69
		m_selected.clear();

		// easy way to traverse darts of orbit
70
		Traversor<MAP>* tra = TraversorFactory<MAP>::createDartsOfOrbits(myMap,m_clicked,orbs[current_orbit]);
71
		for (Dart e = tra->begin(); e != tra->end(); e = tra->next())
Sylvain Thery's avatar
Sylvain Thery committed
72 73 74 75
			m_selected.push_back(e);
	}

	//	m_selected.clear();
76 77 78
	updateGL();
}

79 80
template< unsigned int ORBIT>
void MyQT::storeVerticesInfo(const AttributeHandler<int, ORBIT>* attrib)
81 82
{
	SelectorDartNoBoundary<PFP::MAP> nb(myMap);
Pierre Kraemer's avatar
Pierre Kraemer committed
83
	m_render_topo->computeDartMiddlePositions<PFP>(myMap, middleDarts, nb);
84 85

	m_strings->clear();
Pierre Kraemer's avatar
Pierre Kraemer committed
86
	for (Dart d = myMap.begin(); d != myMap.end(); myMap.next(d))
87 88 89 90
	{
		if (nb(d))
		{
			std::stringstream ss;
91
			ss << (*attrib)[d];
Pierre Kraemer's avatar
Pierre Kraemer committed
92
			m_strings->addString(ss.str(), middleDarts[d]);
93 94 95 96 97
		}
	}
    m_strings->sendToVBO();
}

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138





void MyQT::storeVerticesInfoGen(unsigned int orb, const AttributeHandlerGen* attrib)
{
	switch(orb)
	{
	case VERTEX:
		storeVerticesInfo<VERTEX>(static_cast< const AttributeHandler<int, VERTEX>* >(attrib));
		break;
	case EDGE:
		storeVerticesInfo<EDGE>(static_cast< const AttributeHandler<int, EDGE>* >(attrib));
		break;
	case FACE:
		storeVerticesInfo<FACE>(static_cast< const AttributeHandler<int, FACE>* >(attrib));
		break;
	case VOLUME:
		storeVerticesInfo<VOLUME>(static_cast< const AttributeHandler<int, VOLUME>* >(attrib));
		break;
	case PFP::MAP::VERTEX_OF_PARENT:
		storeVerticesInfo<PFP::MAP::VERTEX_OF_PARENT>(static_cast< const AttributeHandler<int, PFP::MAP::VERTEX_OF_PARENT>* >(attrib));
		break;
	case PFP::MAP::EDGE_OF_PARENT:
		storeVerticesInfo<PFP::MAP::EDGE_OF_PARENT>(static_cast< const AttributeHandler<int, PFP::MAP::EDGE_OF_PARENT>* >(attrib));
		break;
	case PFP::MAP::FACE_OF_PARENT:
		storeVerticesInfo<PFP::MAP::FACE_OF_PARENT>(static_cast< const AttributeHandler<int, PFP::MAP::FACE_OF_PARENT>* >(attrib));
		break;
	case PFP::MAP::VERTEX_OF_PARENT2:
		storeVerticesInfo<PFP::MAP::VERTEX_OF_PARENT2>(static_cast< const AttributeHandler<int, PFP::MAP::VERTEX_OF_PARENT2>* >(attrib));
		break;
	case PFP::MAP::EDGE_OF_PARENT2:
		storeVerticesInfo<PFP::MAP::EDGE_OF_PARENT2>(static_cast< const AttributeHandler<int, PFP::MAP::EDGE_OF_PARENT2>* >(attrib));
		break;
	}
}



139 140 141 142 143 144 145 146 147 148 149 150
void MyQT::cb_initGL()
{
	// choose to use GL version 2
	Utils::GLSLShader::setCurrentOGLVersion(2);

    m_render_topo = new Algo::Render::GL2::Topo3Render();
	SelectorDartNoBoundary<PFP::MAP> nb(myMap);
	m_render_topo->updateData<PFP>(myMap, position,  0.9f, 0.8f, 0.8f, nb);

    m_strings = new Utils::Strings3D(true, Geom::Vec3f(0.1f,0.0f,0.3f));
	registerShader(m_strings);

151
    storeVerticesInfoGen(VERTEX, m_att_orbits[0]);
152 153 154 155 156 157 158
}

void MyQT::cb_redraw()
{
	m_render_topo->drawTopo();

	for (unsigned int i=0; i< m_selected.size(); ++i)
Sylvain Thery's avatar
Sylvain Thery committed
159
		m_render_topo->overdrawDart(m_selected[i], 7, 1.0f, 0.0f, 1.0f);
160 161 162 163 164 165 166 167 168 169 170

	if (render_text)
		m_strings->drawAll(Geom::Vec3f(0.0f, 1.0f, 1.0f));

}

void MyQT::cb_mousePress(int button, int x, int y)
{
	if (Shift())
	{
		SelectorDartNoBoundary<PFP::MAP> nb(myMap);	
Sylvain Thery's avatar
Sylvain Thery committed
171 172
		m_clicked = m_render_topo->picking<PFP>(myMap, x,y, nb);
		if (m_clicked != Dart::nil())
173
		{
174
			unsigned int orbs[9] = {VERTEX,EDGE,FACE,VOLUME,PFP::MAP::VERTEX_OF_PARENT,PFP::MAP::EDGE_OF_PARENT,PFP::MAP::FACE_OF_PARENT,PFP::MAP::VERTEX_OF_PARENT2,PFP::MAP::EDGE_OF_PARENT2};
175 176 177
			m_selected.clear();

			// easy way to traverse darts of orbit
178
			Traversor<MAP>* tra = TraversorFactory<MAP>::createDartsOfOrbits(myMap,m_clicked,orbs[current_orbit]);
179
			for (Dart e = tra->begin(); e != tra->end(); e = tra->next())
180 181 182 183 184 185
				m_selected.push_back(e);
		}
		updateGL();
	}
}

186 187 188
template <unsigned int ORB>
void MyQT::init_att_orb(AttributeHandlerGen* attg)
{
189
	unsigned int i = 0;
190 191 192 193 194 195 196 197 198 199
	TraversorCell<MAP,ORB> tra(myMap);

	AttributeHandler<int,ORB>* att = static_cast< AttributeHandler<int,ORB>* >(attg);

	for (Dart d = tra.begin(); d != tra.end(); d = tra.next())
	{
		(*att)[d] = i++;
	}
}

200 201 202 203
void MyQT::initMap()
{
	std::cout << "INIT MAP"<< std::endl;

Pierre Kraemer's avatar
Pierre Kraemer committed
204
	position = myMap.addAttribute<VEC3, VERTEX>("position");
205 206 207 208 209
	Algo::Modelisation::Primitive3D<PFP> prim(myMap, position);
	int nb=2;
	prim.hexaGrid_topo(nb,nb,nb);
	prim.embedHexaGrid(1.0f,1.0f,1.0f);

210
	m_att_orbits[0] = new AttributeHandler<int, VERTEX>(myMap.addAttribute<int, VERTEX>("vertex"));
211 212 213
	m_att_orbits[1] = new AttributeHandler<int, EDGE>(myMap.addAttribute<int, EDGE>("edge"));
	m_att_orbits[2] = new AttributeHandler<int, FACE>(myMap.addAttribute<int, FACE>("face"));
	m_att_orbits[3] = new AttributeHandler<int, VOLUME>(myMap.addAttribute<int, VOLUME>("volume"));
214 215 216 217 218
	m_att_orbits[4] = new AttributeHandler<int, PFP::MAP::VERTEX_OF_PARENT>(myMap.addAttribute<int, PFP::MAP::VERTEX_OF_PARENT>("vertex2"));
	m_att_orbits[5] = new AttributeHandler<int, PFP::MAP::EDGE_OF_PARENT>(myMap.addAttribute<int, PFP::MAP::EDGE_OF_PARENT>("edge2"));
	m_att_orbits[6] = new AttributeHandler<int, PFP::MAP::FACE_OF_PARENT>(myMap.addAttribute<int, PFP::MAP::FACE_OF_PARENT>("face2"));
	m_att_orbits[7] = new AttributeHandler<int, PFP::MAP::VERTEX_OF_PARENT2>(myMap.addAttribute<int, PFP::MAP::VERTEX_OF_PARENT2>("vertex1"));
	m_att_orbits[8] = new AttributeHandler<int, PFP::MAP::EDGE_OF_PARENT2>(myMap.addAttribute<int, PFP::MAP::EDGE_OF_PARENT2>("face1"));
219

220
	init_att_orb<VERTEX>(m_att_orbits[0]);
221 222 223 224 225 226 227 228
	init_att_orb<EDGE>(m_att_orbits[1]);
	init_att_orb<FACE>(m_att_orbits[2]);
	init_att_orb<VOLUME>(m_att_orbits[3]);
	init_att_orb<PFP::MAP::VERTEX_OF_PARENT>(m_att_orbits[4]);
	init_att_orb<PFP::MAP::EDGE_OF_PARENT>(m_att_orbits[5]);
	init_att_orb<PFP::MAP::FACE_OF_PARENT>(m_att_orbits[6]);
	init_att_orb<PFP::MAP::VERTEX_OF_PARENT2>(m_att_orbits[7]);
	init_att_orb<PFP::MAP::EDGE_OF_PARENT2>(m_att_orbits[8]);
229

Pierre Kraemer's avatar
Pierre Kraemer committed
230
	middleDarts = myMap.addAttribute<VEC3, DART>("middle");
231 232 233 234 235 236 237 238 239 240 241 242 243
}

int main(int argc, char **argv)
{
    // un peu d'interface
	QApplication app(argc, argv);
	MyQT sqt;

	// interface de tuto5.ui
    Utils::QT::uiDockInterface dock;
    sqt.setDock(&dock);

 	// message d'aide
244
	sqt.setHelpMsg("Select an orbit then\nshift click left to select a dart");
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265

	sqt.initMap();

	//  bounding box
    Geom::BoundingBox<PFP::VEC3> bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, position);
    float lWidthObj = std::max<PFP::REAL>(std::max<PFP::REAL>(bb.size(0), bb.size(1)), bb.size(2));
    Geom::Vec3f lPosObj = (bb.min() +  bb.max()) / PFP::REAL(2);

    // envoit info BB a l'interface
	sqt.setParamObject(lWidthObj, lPosObj.data());

	sqt.setCallBack( dock.checkBox_text, SIGNAL(toggled(bool)), SLOT(text_onoff(bool)) );
	sqt.setCallBack( dock.slider_text, SIGNAL(valueChanged(int)), SLOT(slider_text(int)) );
	sqt.setCallBack( dock.Orbits, SIGNAL(	currentIndexChanged(int)), SLOT(orbit_list(int)) );

	sqt.show();
	sqt.slider_text(50);

	// et on attend la fin.
	return app.exec();
}