tuto_orbits.cpp 9.27 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 38
* 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"

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


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;
60 61
	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
62 63 64

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

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

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

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

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

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

97 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





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;
	}
}



138 139 140 141 142 143 144 145 146 147 148 149
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);

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

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
158
		m_render_topo->overdrawDart(m_selected[i], 7, 1.0f, 0.0f, 1.0f);
159 160 161 162 163 164 165 166 167 168 169

	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
170 171
		m_clicked = m_render_topo->picking<PFP>(myMap, x,y, nb);
		if (m_clicked != Dart::nil())
172
		{
173
			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};
174 175 176
			m_selected.clear();

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

185 186 187
template <unsigned int ORB>
void MyQT::init_att_orb(AttributeHandlerGen* attg)
{
188
	unsigned int i = 0;
189 190 191 192 193 194 195 196 197 198
	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++;
	}
}

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

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

209
	m_att_orbits[0] = new AttributeHandler<int, VERTEX>(myMap.addAttribute<int, VERTEX>("vertex"));
210 211 212
	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"));
213 214 215 216 217
	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"));
218

219
	init_att_orb<VERTEX>(m_att_orbits[0]);
220 221 222 223 224 225 226 227
	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]);
228

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

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
243
	sqt.setHelpMsg("Select an orbit then\nshift click left to select a dart");
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264

	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();
}