tuto_orbits.cpp 8.11 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 40 41
MAP myMap;
AttributeHandler<VEC3, VERTEX> position ;
AttributeHandler<VEC3, DART> middleDarts;
42 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)
{
	storeVerticesInfo<int>(m_att_orbits[x]);
	current_orbit = x;
Sylvain Thery's avatar
Sylvain Thery committed
61 62 63

	if (m_clicked != Dart::nil())
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
64
		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)};
Sylvain Thery's avatar
Sylvain Thery committed
65 66 67
		m_selected.clear();

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

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

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

	m_strings->clear();
Pierre Kraemer's avatar
Pierre Kraemer committed
84
	for (Dart d = myMap.begin(); d != myMap.end(); myMap.next(d))
85 86 87 88 89
	{
		if (nb(d))
		{
			std::stringstream ss;
			ss << attrib[d];
Pierre Kraemer's avatar
Pierre Kraemer committed
90
			m_strings->addString(ss.str(), middleDarts[d]);
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
		}
	}
    m_strings->sendToVBO();
}

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

    storeVerticesInfo<int>(m_att_orbits[0]);
}

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
116
		m_render_topo->overdrawDart(m_selected[i], 7, 1.0f, 0.0f, 1.0f);
117 118 119 120 121 122 123 124 125 126 127

	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
128 129
		m_clicked = m_render_topo->picking<PFP>(myMap, x,y, nb);
		if (m_clicked != Dart::nil())
130
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
131
			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)};
132 133 134
			m_selected.clear();

			// easy way to traverse darts of orbit
Pierre Kraemer's avatar
Pierre Kraemer committed
135
			TraversorDartsOfOrbit<PFP::MAP> tra(myMap, orbs[current_orbit], m_clicked);
136 137 138 139 140 141 142 143 144 145 146
			for (Dart e = tra.begin(); e != tra.end(); e = tra.next())
				m_selected.push_back(e);
		}
		updateGL();
	}
}

void MyQT::initMap()
{
	std::cout << "INIT MAP"<< std::endl;

Pierre Kraemer's avatar
Pierre Kraemer committed
147
	position = myMap.addAttribute<VEC3, VERTEX>("position");
148 149 150 151 152
	Algo::Modelisation::Primitive3D<PFP> prim(myMap, position);
	int nb=2;
	prim.hexaGrid_topo(nb,nb,nb);
	prim.embedHexaGrid(1.0f,1.0f,1.0f);

Pierre Kraemer's avatar
Pierre Kraemer committed
153 154 155 156 157 158 159 160 161
	m_att_orbits[0] = myMap.addAttribute<int, VERTEX>("vertex");
	m_att_orbits[1] = myMap.addAttribute<int, EDGE>("edge");
	m_att_orbits[2] = myMap.addAttribute<int, FACE>("face");
	m_att_orbits[3] = myMap.addAttribute<int, VOLUME>("volume");
	m_att_orbits[4] = myMap.addAttribute<int, VERTEX + PFP::MAP::IN_PARENT>("vertex2");
	m_att_orbits[5] = myMap.addAttribute<int, EDGE + PFP::MAP::IN_PARENT>("edge2");
	m_att_orbits[6] = myMap.addAttribute<int, FACE + PFP::MAP::IN_PARENT>("face2");
	m_att_orbits[7] = myMap.addAttribute<int, VERTEX + PFP::MAP::IN_PARENT2>("vertex1");
	m_att_orbits[8] = myMap.addAttribute<int, EDGE + PFP::MAP::IN_PARENT2>("face1");
162 163

	int i=0;
Pierre Kraemer's avatar
Pierre Kraemer committed
164
	TraversorV<MAP> tra0(myMap);
165 166 167 168 169 170
	for (Dart d = tra0.begin(); d != tra0.end(); d = tra0.next())
	{
		m_att_orbits[0][d] = i++;
	}

	i=0;
Pierre Kraemer's avatar
Pierre Kraemer committed
171
	TraversorE<MAP> tra1(myMap);
172 173 174 175 176 177
	for (Dart d = tra1.begin(); d != tra1.end(); d = tra1.next())
	{
		m_att_orbits[1][d] = i++;
	}

	i=0;
Pierre Kraemer's avatar
Pierre Kraemer committed
178
	TraversorF<MAP> tra2(myMap);
179 180 181 182 183 184
	for (Dart d = tra2.begin(); d != tra2.end(); d = tra2.next())
	{
		m_att_orbits[2][d] = i++;
	}

	i=0;
Pierre Kraemer's avatar
Pierre Kraemer committed
185
	TraversorW<PFP::MAP> tra3(myMap);
186 187 188 189 190 191
	for (Dart d = tra3.begin(); d != tra3.end(); d = tra3.next())
	{
		m_att_orbits[3][d] = i++;
	}

	i=0;
Pierre Kraemer's avatar
Pierre Kraemer committed
192
	TraversorCell<MAP, VERTEX + PFP::MAP::IN_PARENT> tra02(myMap);
193 194 195 196 197 198
	for (Dart d = tra02.begin(); d != tra02.end(); d = tra02.next())
	{
		m_att_orbits[4][d] = i++;
	}

	i=0;
Pierre Kraemer's avatar
Pierre Kraemer committed
199
	TraversorCell<MAP, EDGE + PFP::MAP::IN_PARENT> tra12(myMap);
200 201 202 203 204 205
	for (Dart d = tra12.begin(); d != tra12.end(); d = tra12.next())
	{
		m_att_orbits[5][d] = i++;
	}

	i=0;
Pierre Kraemer's avatar
Pierre Kraemer committed
206
	TraversorCell<MAP, FACE + PFP::MAP::IN_PARENT> tra22(myMap);
207 208 209 210 211 212
	for (Dart d = tra22.begin(); d != tra22.end(); d = tra22.next())
	{
		m_att_orbits[6][d] = i++;
	}

	i=0;
Pierre Kraemer's avatar
Pierre Kraemer committed
213
	TraversorCell<MAP, VERTEX + PFP::MAP::IN_PARENT2> tra01(myMap);
214 215 216 217 218 219
	for (Dart d = tra01.begin(); d != tra01.end(); d = tra01.next())
	{
		m_att_orbits[7][d] = i++;
	}

	i=0;
Pierre Kraemer's avatar
Pierre Kraemer committed
220
	TraversorCell<MAP, EDGE + PFP::MAP::IN_PARENT2> tra11(myMap);
221 222 223 224 225
	for (Dart d = tra11.begin(); d != tra11.end(); d = tra11.next())
	{
		m_att_orbits[8][d] = i++;
	}

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

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

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