diff --git a/CMakeLists.txt b/CMakeLists.txt index 085538575b81897f14c09b9ec1521067a37f6153..7658ad540d148fce848ad80c5fc9a6373aa3ea20 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.8) project(SocialAgents) -#add_definitions(-DSPATIAL_HASHING) +add_definitions(-DSPATIAL_HASHING) SET(CGoGN_ROOT_DIR ${CMAKE_SOURCE_DIR}/../CGoGN CACHE STRING "CGoGN root dir") include(${CGoGN_ROOT_DIR}/apps_cmake.txt) diff --git a/include/agent.h b/include/agent.h index e49f2445099f23a09e816189f7ef4c13b31d454e..e67a76d7fca70d629eed26a16bce08ab63c643d1 100644 --- a/include/agent.h +++ b/include/agent.h @@ -8,7 +8,7 @@ #include "spatialHashing.h" #include "Algo/MovingObjects/particle_cell_2D.h" -class Simulator; +class Simulator ; class Agent { @@ -16,78 +16,69 @@ public: #ifdef SPATIAL_HASHING Agent(Simulator* sim, const VEC3& position) ; #else - Agent(Simulator* sim, const VEC3& position, Dart d); + Agent(Simulator* sim, const VEC3& position, Dart d) ; #endif - VEC3 getPosition(); + VEC3 getPosition() ; - void updateAgentNeighbors(); - void updateObstacleNeighbors(); + void updateAgentNeighbors() ; + void updateObstacleNeighbors() ; - void update(); - - void computePrefVelocity(); - void computeNewVelocity(); + void update() ; - void computeNewVelocity2(); + void computePrefVelocity() ; + void computeNewVelocity() ; - bool linearProgram1( - const std::vector& lines, unsigned int lineNo, - float radius, const VEC3& optVelocity, - bool directionOpt, VEC3& result - ); + void computeNewVelocity2() ; - unsigned int linearProgram2( - const std::vector& lines, float radius, - const VEC3& optVelocity, bool directionOpt, - VEC3& result - ); + bool linearProgram1(const std::vector& lines, unsigned int lineNo, float radius, + const VEC3& optVelocity, bool directionOpt, VEC3& result) ; - void linearProgram3( - const std::vector& lines, unsigned int numObstLines, - unsigned int beginLine, float radius, - VEC3& result - ); - - std::vector > agentNeighbors_; - std::vector > obstacleNeighbors_; + unsigned int linearProgram2(const std::vector& lines, float radius, + const VEC3& optVelocity, bool directionOpt, VEC3& result) ; + + void linearProgram3(const std::vector& lines, unsigned int numObstLines, + unsigned int beginLine, float radius, VEC3& result) ; + + std::vector > agentNeighbors_ ; + std::vector > obstacleNeighbors_ ; #ifdef SPATIAL_HASHING VEC3 pos ; #else - CGoGN::Algo::MovingObjects::ParticleCell2D part_; + CGoGN::Algo::MovingObjects::ParticleCell2D part_ ; #endif - - std::vector goals_; - - VEC3 finalGoal; - Dart finalDart; - - unsigned int curGoal_; - - static unsigned int maxNeighbors_; - static float maxSpeed_; - static float neighborDist_; - static float neighborDistSq_; - static float radius_; - static float timeHorizon_; - static float timeHorizonObst_; - static float range_; - static float rangeSq_; + + std::vector goals_ ; + + VEC3 finalGoal ; + Dart finalDart ; + + unsigned int curGoal_ ; + + static unsigned int maxNeighbors_ ; + static float maxSpeed_ ; + static float neighborDist_ ; + static float neighborDistSq_ ; + static float radius_ ; + static float timeHorizon_ ; + static float timeHorizonObst_ ; + static float range_ ; + static float rangeSq_ ; static float timeStep ; - + static unsigned int cptAgent ; unsigned int agentNo ; - VEC3 velocity_; - VEC3 newVelocity_; - VEC3 prefVelocity_; - - VEC3 meanSpeed_; + VEC3 velocity_ ; + VEC3 newVelocity_ ; + VEC3 prefVelocity_ ; + + VEC3 meanSpeed_ ; // VEC3 meanPos_; - Simulator* sim_; -}; + Simulator* sim_ ; +} ; #endif diff --git a/include/env_generator.h b/include/env_generator.h index 9327d1479d14211ea991284ef7df385fa40842ce..2d5ad63e5839c47c874bfc82abd18d6918a5e96c 100644 --- a/include/env_generator.h +++ b/include/env_generator.h @@ -10,24 +10,26 @@ namespace CGoGN namespace CityGenerator { -template +template bool notDiagonalAdjacentToAnObstacle(typename PFP::MAP& map, Dart d, CellMarker& buildingMark) ; -template Algo::Modelisation::Polyhedron generateGrid(EnvMap& envMap) ; +template Algo::Modelisation::Polyhedron generateGrid(EnvMap& envMap) ; -template void generateCity(EnvMap& envMap) ; +template void generateCity(EnvMap& envMap) ; -template +template Algo::Modelisation::Polyhedron generateTrianGrid(typename PFP::MAP& map, - typename PFP::TVEC3& position, unsigned int cX, unsigned int cY, float sideLength, - CellMarker& obstacleMark, CellMarker& buildingMark) ; + typename PFP::TVEC3& position, + unsigned int cX, unsigned int cY, + float sideLength, CellMarker& obstacleMark, + CellMarker& buildingMark) ; -template +template Dart extrudeFaceAndMark(typename PFP::MAP& map, typename PFP::TVEC3& position, Dart d, - CellMarker& buildingMark, float height) ; + CellMarker& buildingMark, float height) ; #ifndef SPATIAL_HASHING -template +template bool animateCity(EnvMap* envMap) ; #endif @@ -37,43 +39,42 @@ bool animateCity(EnvMap* envMap) ; //template //void generateCity(typename PFP::MAP& map, typename PFP::TVEC3& position, CellMarker& obstacleMark, CellMarker& buildingMark, float sideSize, unsigned int nbSquares); -template +template Dart generateBuilding(EnvMap& envMap, Dart d, float height, unsigned int buildingType) ; -template -void generateMall(typename PFP::MAP& map, typename PFP::TVEC3& position, - CellMarker& obstacleMark, CellMarker& buildingMark, float sideSize) ; +template +void generateMall(typename PFP::MAP& map, typename PFP::TVEC3& position, CellMarker& obstacleMark, + CellMarker& buildingMark, float sideSize) ; -template -void generatePathToUpperStair(typename PFP::MAP& map, typename PFP::TVEC3& position, - Dart dLower, Dart dUpper) ; +template +void generatePathToUpperStair(typename PFP::MAP& map, typename PFP::TVEC3& position, Dart dLower, + Dart dUpper) ; /*******************************************************************************/ -template -void generatePlanet(typename PFP::MAP& map, typename PFP::TVEC3& position, - CellMarker& obstacleMark, CellMarker& buildingMark, float radius, - unsigned int nbSquares) ; +template +void generatePlanet(typename PFP::MAP& map, typename PFP::TVEC3& position, CellMarker& obstacleMark, + CellMarker& buildingMark, float radius, unsigned int nbSquares) ; /*******************************************************************************/ -template +template void simplifyFreeSpace(typename PFP::MAP& map, typename PFP::TVEC3& position) ; -template +template bool isAnEar(typename PFP::MAP& map, typename PFP::TVEC3& position, Dart dd, float& area) ; -template +template bool canRemoveEdgeConvex(typename PFP::MAP& map, typename PFP::TVEC3& position, Dart d, - CellMarker& obstacleMark) ; + CellMarker& obstacleMark) ; -template +template void convexifyFreeSpace(typename PFP::MAP& map, typename PFP::TVEC3& position, - CellMarker& obstacleMark, CellMarker& buildingMark) ; + CellMarker& obstacleMark, CellMarker& buildingMark) ; -template +template void installGuardRail(typename PFP::MAP& map, typename PFP::TVEC3& position, - CellMarker& obstacleMark, CellMarker& buildingMark, float height) ; + CellMarker& obstacleMark, CellMarker& buildingMark, float height) ; } diff --git a/include/env_generator.hpp b/include/env_generator.hpp index 0919e357d8167a987b43c03bb3df68776477e96c..a0b081e833f48530a606e9222f6f580143bbe94c 100644 --- a/include/env_generator.hpp +++ b/include/env_generator.hpp @@ -9,11 +9,12 @@ namespace CGoGN namespace CityGenerator { -template +template bool notAdjacentToAnObstacle(typename PFP::MAP& map, Dart d, CellMarker& buildingMark) { Dart dd = d ; - do { + do + { if (buildingMark.isMarked(dd)) return false ; dd = map.phi1(dd) ; } while (dd != d) ; @@ -21,11 +22,12 @@ bool notAdjacentToAnObstacle(typename PFP::MAP& map, Dart d, CellMarker& buildin return true ; } -template +template bool notDiagonalAdjacentToAnObstacle(typename PFP::MAP& map, Dart d, CellMarker& buildingMark) { Dart dd = d ; - do { + do + { if (buildingMark.isMarked(map.alpha1(map.alpha1(dd)))) return false ; dd = map.phi1(dd) ; } while (dd != d) ; @@ -33,15 +35,15 @@ bool notDiagonalAdjacentToAnObstacle(typename PFP::MAP& map, Dart d, CellMarker& return true ; } -template +template Algo::Modelisation::Polyhedron generateGrid(EnvMap& envMap) { unsigned int nx = envMap.geometry.size(0) / envMap.maxCellSize ; unsigned int ny = envMap.geometry.size(1) / envMap.maxCellSize ; - if (nx < 1) nx = 1; - if (ny < 1) ny = 1; + if (nx < 1) nx = 1 ; + if (ny < 1) ny = 1 ; - std::cout << " - Generate Grid : " << nx << " x " << ny << std::endl; + std::cout << " - Generate Grid : " << nx << " x " << ny << std::endl ; Algo::Modelisation::Polyhedron prim(envMap.map, envMap.position) ; Dart d = prim.grid_topo(nx, ny) ; @@ -51,7 +53,8 @@ Algo::Modelisation::Polyhedron generateGrid(EnvMap& envMap) envMap.buildingMark.mark(boundary) ; Dart e = boundary ; - do { + do + { envMap.obstacleMark.mark(e) ; e = envMap.map.phi1(e) ; } while (e != boundary) ; @@ -59,28 +62,32 @@ Algo::Modelisation::Polyhedron generateGrid(EnvMap& envMap) return prim ; } -template +template void generateCity(EnvMap& envMap) { unsigned int nbBuilding = 1000 ; - std::cout << " - Generate City : " << nbBuilding << " buildings" << std::endl; + std::cout << " - Generate City : " << nbBuilding << " buildings" << std::endl ; generateGrid(envMap) ; Dart dEnd = envMap.map.end() ; - for (Dart d = envMap.map.begin() ; d != dEnd && nbBuilding > 0 ; envMap.map.next(d)) { - if ( !envMap.buildingMark.isMarked(d) && (rand() % 12 == 0) - && notDiagonalAdjacentToAnObstacle(envMap.map, d, envMap.buildingMark)) { + for (Dart d = envMap.map.begin(); d != dEnd && nbBuilding > 0; envMap.map.next(d)) + { + if (!envMap.buildingMark.isMarked(d) && (rand() % 12 == 0) + && notDiagonalAdjacentToAnObstacle(envMap.map, d, envMap.buildingMark)) + { generateBuilding(envMap, d, (1 + (rand() % 3)) * 2.0f, rand() % 4) ; --nbBuilding ; } } } -template +template Algo::Modelisation::Polyhedron generateTrianGrid(typename PFP::MAP& map, - typename PFP::TVEC3& position, unsigned int cX, unsigned int cY, float sideLength, - CellMarker& obstacleMark, CellMarker& buildingMark) + typename PFP::TVEC3& position, + unsigned int cX, unsigned int cY, + float sideLength, CellMarker& obstacleMark, + CellMarker& buildingMark) { Algo::Modelisation::Polyhedron prim(map, position) ; prim.grid_topo(cX, cY) ; @@ -90,11 +97,13 @@ Algo::Modelisation::Polyhedron generateTrianGrid(typename PFP::MAP& map, Dart dY = prim.getDart() ; //remind the first quad of the line Dart dX = prim.getDart() ; //goes through the line bool odd = true ; //odd line or not - for (unsigned int i = 0 ; i < cX * cY ;) { + for (unsigned int i = 0; i < cX * cY;) + { Dart dNext = map.phi1(map.phi2(map.phi1(dX))) ; Dart toCut = dX ; - if (odd) { + if (odd) + { toCut = map.phi1(toCut) ; //change the side of the split face position[toCut][0] -= sideLength / 2.0f ; //move vertices for equilateral triangles } @@ -104,37 +113,46 @@ Algo::Modelisation::Polyhedron generateTrianGrid(typename PFP::MAP& map, ++i ; if (i % cX == 0 && i > 0) //goes up and change side of split - { + { Dart endSquare = map.newOrientedFace(3) ; //add triangle add end of lines to make a square Dart dN ; - if (odd) { + if (odd) + { dN = map.phi1(map.phi2(map.phi1(dX))) ; map.sewFaces(dN, endSquare) ; position[map.phi_1(endSquare)] = position[map.phi1(endSquare)] ; position[map.phi_1(endSquare)][0] += sideLength / 2.0f ; } - else { + else + { dN = map.phi1(dX) ; map.sewFaces(dN, endSquare) ; position[map.phi_1(endSquare)] = position[endSquare] ; } - if (odd) dY = map.phi2(map.phi_1(map.phi2(map.phi1(dY)))) ; - else dY = map.phi2(map.phi1(map.phi2(map.phi_1(dY)))) ; + if (odd) + dY = map.phi2(map.phi_1(map.phi2(map.phi1(dY)))) ; + else + dY = map.phi2(map.phi1(map.phi2(map.phi_1(dY)))) ; dX = dY ; odd = !odd ; } - else dX = dNext ; + else + dX = dNext ; } Dart boundary ; - for (Dart d = map.begin() ; d != map.end() ; map.next(d)) { - if (map.phi2(d) == d) { + for (Dart d = map.begin(); d != map.end(); map.next(d)) + { + if (map.phi2(d) == d) + { Dart dA = map.alpha1(map.phi1(d)) ; if (map.phi2(dA) == dA && position[dA] == position[map.phi1(d)] - && position[map.phi1(dA)] == position[d]) map.sewFaces(dA, d) ; - else { + && position[map.phi1(dA)] == position[d]) + map.sewFaces(dA, d) ; + else + { obstacleMark.mark(d) ; boundary = d ; } @@ -146,7 +164,8 @@ Algo::Modelisation::Polyhedron generateTrianGrid(typename PFP::MAP& map, if (odd) //last top line if odd { - for (unsigned int i = 0 ; i < cX ; ++i) { + for (unsigned int i = 0; i < cX; ++i) + { dX = map.phi1(dX) ; position[dX][0] -= sideLength / 2.0f ; } @@ -164,14 +183,15 @@ Algo::Modelisation::Polyhedron generateTrianGrid(typename PFP::MAP& map, return prim ; } -template +template Dart extrudeFaceAndMark(typename PFP::MAP& map, typename PFP::TVEC3& position, Dart d, - CellMarker& buildingMark, float height) + CellMarker& buildingMark, float height) { Dart dRoof = Algo::Modelisation::extrudeFace(map, position, d, height) ; buildingMark.mark(dRoof) ; Dart dd = dRoof ; - do { + do + { buildingMark.mark(map.phi2(dd)) ; dd = map.phi1(dd) ; } while (dd != dRoof) ; @@ -179,7 +199,7 @@ Dart extrudeFaceAndMark(typename PFP::MAP& map, typename PFP::TVEC3& position, D } #ifndef SPATIAL_HASHING -template +template bool animateCity(EnvMap* envMap) { typename PFP::MAP& map = envMap->map ; @@ -191,20 +211,23 @@ bool animateCity(EnvMap* envMap) std::vector& newBuildings = envMap->newBuildings ; unsigned int state = rand() % 10 ; - if (state < 2) { + if (state < 2) + { map.setCurrentLevel(0) ; //generate new building Dart d(rand() % map.getNbDarts()) ; - if ( !map.isDartValid(d)) map.next(d) ; + if (!map.isDartValid(d)) map.next(d) ; while (d != map.end() && map.getDartLevel(d) > 0) map.next(d) ; - if (d != map.end()) { - if ( !buildingMark.isMarked(d) && agents[d].size() == 0 - && agentNeighbors[d].size() == 0 && !map.faceIsSubdivided(d) - && notAdjacentToAnObstacle(map, d, obstacleMark) - && notDiagonalAdjacentToAnObstacle(map, d, obstacleMark)) { + if (d != map.end()) + { + if (!buildingMark.isMarked(d) && agents[d].size() == 0 && agentNeighbors[d].size() == 0 + && !map.faceIsSubdivided(d) + && notAdjacentToAnObstacle(map, d, obstacleMark) + && notDiagonalAdjacentToAnObstacle(map, d, obstacleMark)) + { map.setCurrentLevel(map.getMaxLevel()) ; d = generateBuilding(envMap, d, 2.0f, 0) ; newBuildings.push_back(d) ; @@ -214,33 +237,37 @@ bool animateCity(EnvMap* envMap) map.setCurrentLevel(map.getMaxLevel()) ; } - else if (state < 6 && newBuildings.size() > 0) { + else if (state < 6 && newBuildings.size() > 0) + { //take a newBuilding and make it higher state = rand() % newBuildings.size() ; Dart d = newBuildings[state] ; //decide if we create a new floor, or just make it higher unsigned int typeOfUpdate = rand() % 10 ; - if (typeOfUpdate < 9) { + if (typeOfUpdate < 9) + { Dart dd = d ; - do { + do + { position[dd][2] += 2.0f ; dd = map.phi1(dd) ; } while (dd != d) ; - if (position[dd][2] > ((10 + rand() % 10) * 10.0f)) - newBuildings.erase(newBuildings.begin() + state) ; + if (position[dd][2] > ((10 + rand() % 10) * 10.0f)) newBuildings.erase( + newBuildings.begin() + state) ; } - else { + else + { typename PFP::VEC3 c = Algo::Geometry::faceCentroid(map, d, position) ; Dart dRoofSmall = Algo::Modelisation::extrudeFace(map, position, d, 0.0f) ; Dart dd = dRoofSmall ; - do { + do + { position[dd] = position[dd] + (c - position[dd]) / 3.0f ; dd = map.phi1(dd) ; } while (dd != dRoofSmall) ; // Dart dRoof = Algo::Modelisation::extrudeFace(map,position,dRoofSmall,2.0f); - Dart dRoof = extrudeFaceAndMark(map, position, dRoofSmall, buildingMark, - 2.0f) ; + Dart dRoof = extrudeFaceAndMark(map, position, dRoofSmall, buildingMark, 2.0f) ; newBuildings.erase(newBuildings.begin() + state) ; newBuildings.push_back(dRoof) ; @@ -257,7 +284,7 @@ bool animateCity(EnvMap* envMap) //template //void generateCity(typename PFP::MAP& map, typename PFP::TVEC3& position, CellMarker& obstacleMark, CellMarker& buildingMark, float sideSize, unsigned int nbSquares) -template +template Dart generateBuilding(EnvMap& envMap, Dart d, float height, unsigned int buildingType) { typename PFP::MAP& map = envMap.map ; @@ -268,13 +295,14 @@ Dart generateBuilding(EnvMap& envMap, Dart d, float height, unsigned int buildin // mark the face as obstacle before extrusion Dart dd = d ; buildingMark.mark(dd) ; - do { + do + { obstacleMark.mark(dd) ; Dart next = map.phi1(dd) ; Dart previous = map.phi_1(dd) ; Obstacle* o = new Obstacle(position[dd], position[next], position[previous], - position[map.phi1(next)]) ; + position[map.phi1(next)]) ; #ifdef SPATIAL_HASHING VEC3 ov = o->p2 - o->p1 ; @@ -310,85 +338,95 @@ Dart generateBuilding(EnvMap& envMap, Dart d, float height, unsigned int buildin Dart dRoof ; dRoof = extrudeFaceAndMark(map, position, d, buildingMark, height) ; - switch (buildingType) { - case 0 : { - break ; - } - case 1 : { - dRoof = extrudeFaceAndMark(map, position, dRoof, buildingMark, height / 3) ; - Dart dNext = map.phi1(dRoof) ; - Dart dPrev = map.phi2(map.phi_1(dRoof)) ; - map.collapseEdge(dNext) ; - map.collapseEdge(dPrev) ; - break ; - } - case 2 : { - dRoof = extrudeFaceAndMark(map, position, dRoof, buildingMark, height / 3) ; - Dart dNext = map.phi1(dRoof) ; - Dart dPrev = map.phi2(map.phi_1(dRoof)) ; - typename PFP::VEC3 mid1 = (position[dNext] + position[map.phi1(dNext)]) / 2.0f ; - typename PFP::VEC3 mid2 = (position[dPrev] + position[map.phi1(dPrev)]) / 2.0f ; - map.collapseEdge(dNext) ; - map.collapseEdge(dPrev) ; - position[dRoof] = mid2 ; - position[map.phi1(dRoof)] = mid1 ; - break ; + switch (buildingType) + { + case 0 : + { + break ; + } + case 1 : + { + dRoof = extrudeFaceAndMark(map, position, dRoof, buildingMark, height / 3) ; + Dart dNext = map.phi1(dRoof) ; + Dart dPrev = map.phi2(map.phi_1(dRoof)) ; + map.collapseEdge(dNext) ; + map.collapseEdge(dPrev) ; + break ; + } + case 2 : + { + dRoof = extrudeFaceAndMark(map, position, dRoof, buildingMark, height / 3) ; + Dart dNext = map.phi1(dRoof) ; + Dart dPrev = map.phi2(map.phi_1(dRoof)) ; + typename PFP::VEC3 mid1 = (position[dNext] + position[map.phi1(dNext)]) / 2.0f ; + typename PFP::VEC3 mid2 = (position[dPrev] + position[map.phi1(dPrev)]) / 2.0f ; + map.collapseEdge(dNext) ; + map.collapseEdge(dPrev) ; + position[dRoof] = mid2 ; + position[map.phi1(dRoof)] = mid1 ; + break ; + } + case 3 : + { + unsigned int nbStairs = rand() % 5 ; + for (unsigned int i = 0; i < nbStairs; ++i) + { + typename PFP::VEC3 c = Algo::Geometry::faceCentroid(map, dRoof, position) ; + Dart dRoofSmall = extrudeFaceAndMark(map, position, dRoof, buildingMark, 0.0f) ; + Dart dd = dRoofSmall ; + do + { + position[dd] = position[dd] + (c - position[dd]) / 3.0f ; + dd = map.phi1(dd) ; + } while (dd != dRoofSmall) ; + dRoof = extrudeFaceAndMark(map, position, dRoofSmall, buildingMark, + height / 2.0f) ; } - case 3 : { - unsigned int nbStairs = rand() % 5 ; - for (unsigned int i = 0 ; i < nbStairs ; ++i) { - typename PFP::VEC3 c = Algo::Geometry::faceCentroid(map, dRoof, position) ; - Dart dRoofSmall = extrudeFaceAndMark(map, position, dRoof, buildingMark, - 0.0f) ; - Dart dd = dRoofSmall ; - do { - position[dd] = position[dd] + (c - position[dd]) / 3.0f ; - dd = map.phi1(dd) ; - } while (dd != dRoofSmall) ; - dRoof = extrudeFaceAndMark(map, position, dRoofSmall, buildingMark, - height / 2.0f) ; - } - bool spike = rand() % 2 ; - if (spike) { - typename PFP::VEC3 c = Algo::Geometry::faceCentroid(map, dRoof, position) ; - c[2] += height / 1.5f ; - dRoof = Algo::Modelisation::trianguleFace(map, dRoof) ; - position[dRoof] = c ; - } - break ; + bool spike = rand() % 2 ; + if (spike) + { + typename PFP::VEC3 c = Algo::Geometry::faceCentroid(map, dRoof, position) ; + c[2] += height / 1.5f ; + dRoof = Algo::Modelisation::trianguleFace(map, dRoof) ; + position[dRoof] = c ; } + break ; + } } return dRoof ; } -template -void generateMall(typename PFP::MAP& map, typename PFP::TVEC3& position, - CellMarker& obstacleMark, CellMarker& buildingMark, float sideSize) +template +void generateMall(typename PFP::MAP& map, typename PFP::TVEC3& position, CellMarker& obstacleMark, + CellMarker& buildingMark, float sideSize) { unsigned int side = 5 ; std::vector > floors ; - Algo::Modelisation::Polyhedron * floor2 = new Algo::Modelisation::Polyhedron(map, - position) ; + Algo::Modelisation::Polyhedron * floor2 = new Algo::Modelisation::Polyhedron( + map, position) ; floor2->grid_topo(side, side) ; floor2->embedGrid(sideSize * side, sideSize * side) ; - for (unsigned int i = 0 ; i < 3 ; ++i) { + for (unsigned int i = 0; i < 3; ++i) + { float floorHeight = 100 ; typename PFP::VEC3 transl(0, 0, floorHeight) ; CellMarker m(map, VERTEX) ; - for (Dart d = map.begin() ; d != map.end() ; map.next(d)) { - if ( !m.isMarked(d)) { + for (Dart d = map.begin(); d != map.end(); map.next(d)) + { + if (!m.isMarked(d)) + { m.mark(d) ; position[d] += transl ; } } Algo::Modelisation::Polyhedron * floor1 = new Algo::Modelisation::Polyhedron( - map, position) ; + map, position) ; floor1->grid_topo(side, side) ; floor1->embedGrid(sideSize * side, sideSize * side) ; @@ -401,8 +439,10 @@ void generateMall(typename PFP::MAP& map, typename PFP::TVEC3& position, } Dart boundary ; - for (Dart d = map.begin() ; d != map.end() ; map.next(d)) { - if (map.phi2(d) == d) { + for (Dart d = map.begin(); d != map.end(); map.next(d)) + { + if (map.phi2(d) == d) + { obstacleMark.mark(d) ; boundary = d ; } @@ -414,14 +454,15 @@ void generateMall(typename PFP::MAP& map, typename PFP::TVEC3& position, installGuardRail(map, position, obstacleMark, buildingMark, sideSize / 10.0f) ; } -template -void generatePathToUpperStair(typename PFP::MAP& map, typename PFP::TVEC3& position, - Dart dLower, Dart dUpper) +template +void generatePathToUpperStair(typename PFP::MAP& map, typename PFP::TVEC3& position, Dart dLower, + Dart dUpper) { - if (dLower != map.phi2(dLower) || dUpper != map.phi2(dUpper)) { + if (dLower != map.phi2(dLower) || dUpper != map.phi2(dUpper)) + { std::cout - << "generatePathToUpperStair : lower and upper stair darts must be fixpoint in phi2" - << std::endl ; + << "generatePathToUpperStair : lower and upper stair darts must be fixpoint in phi2" + << std::endl ; return ; } @@ -445,8 +486,7 @@ void generatePathToUpperStair(typename PFP::MAP& map, typename PFP::TVEC3& posit float z = (position[dLower][2] + position[dUpper][2]) / 2.0f ; //from lower path - typename PFP::VEC3 vDown = position[map.phi1(dLower)] - - position[map.phi1(map.phi1(dLower))] ; + typename PFP::VEC3 vDown = position[map.phi1(dLower)] - position[map.phi1(map.phi1(dLower))] ; position[map.phi1(map.phi1(dInBetweenDown))] = position[map.phi1(dLower)] + vDown ; position[map.phi_1(dInBetweenDown)] = position[dLower] + vDown ; @@ -462,22 +502,23 @@ void generatePathToUpperStair(typename PFP::MAP& map, typename PFP::TVEC3& posit position[map.phi1(dInBetweenUp)] = position[map.phi1(map.phi1(dInBetweenUp))] + vDown ; Dart dd = dInBetweenUp ; - do { + do + { position[dd][2] = z ; dd = map.phi1(dd) ; } while (dd != dInBetweenUp) ; dd = dInBetweenDown ; - do { + do + { position[dd][2] = z ; dd = map.phi1(dd) ; } while (dd != dInBetweenDown) ; } -template -void generatePlanet(typename PFP::MAP& map, typename PFP::TVEC3& position, - CellMarker& obstacleMark, CellMarker& buildingMark, float radius, - unsigned int nbSquares) +template +void generatePlanet(typename PFP::MAP& map, typename PFP::TVEC3& position, CellMarker& obstacleMark, + CellMarker& buildingMark, float radius, unsigned int nbSquares) { Algo::Modelisation::Polyhedron prim(map, position) ; prim.cylinder_topo(nbSquares, nbSquares, true, true) ; @@ -485,9 +526,9 @@ void generatePlanet(typename PFP::MAP& map, typename PFP::TVEC3& position, prim.embedSphere(radius) ; } -template +template void simplifyFreeSpace(typename PFP::MAP& map, typename PFP::TVEC3& position, - CellMarker& obstacleMark, CellMarker& buildingMark) + CellMarker& obstacleMark, CellMarker& buildingMark) { typedef typename PFP::VEC3 VEC3 ; typedef std::multimap VEC_D_A ; @@ -500,11 +541,12 @@ void simplifyFreeSpace(typename PFP::MAP& map, typename PFP::TVEC3& position, // compute all heuristic and construct multimap Algo::Geometry::computeAreaFaces(map, position, tableArea) ; CellMarker eM(map, EDGE) ; - for (Dart d = map.begin() ; d != map.end() ; map.next(d)) { - if ( !eM.isMarked(d) && !obstacleMark.isMarked(d) && !buildingMark.isMarked(d)) { + for (Dart d = map.begin(); d != map.end(); map.next(d)) + { + if (!eM.isMarked(d) && !obstacleMark.isMarked(d) && !buildingMark.isMarked(d)) + { eM.mark(d) ; - tableIt[d] = orderedD.insert( - std::make_pair(tableArea[d] + tableArea[map.phi2(d)], d)) ; + tableIt[d] = orderedD.insert(std::make_pair(tableArea[d] + tableArea[map.phi2(d)], d)) ; } } eM.unmarkAll() ; @@ -513,9 +555,11 @@ void simplifyFreeSpace(typename PFP::MAP& map, typename PFP::TVEC3& position, VEC_D_A::iterator it = orderedD.begin() ; if (it == orderedD.end()) stillSimplif = false ; - while (stillSimplif) { + while (stillSimplif) + { Dart d = it->second ; - if ( !map.sameFace(d, map.phi2(d))) { + if (!map.sameFace(d, map.phi2(d))) + { // remove selected edge float sumArea = it->first ; orderedD.erase(it) ; @@ -525,8 +569,10 @@ void simplifyFreeSpace(typename PFP::MAP& map, typename PFP::TVEC3& position, // erase all edges of new face from multimap Dart ddd = dd ; - do { - if ( !eM.isMarked(ddd) && !obstacleMark.isMarked(ddd)) { + do + { + if (!eM.isMarked(ddd) && !obstacleMark.isMarked(ddd)) + { eM.mark(ddd) ; orderedD.erase(tableIt[ddd]) ; } @@ -535,11 +581,13 @@ void simplifyFreeSpace(typename PFP::MAP& map, typename PFP::TVEC3& position, // collapse edges in faces if possible ddd = dd ; - do { + do + { Dart dN = map.phi1(ddd) ; // remove pending edges - if (map.phi2(dN) == map.phi1(dN)) { + if (map.phi2(dN) == map.phi1(dN)) + { if (dN == dd || map.phi1(dN) == dd) dd = map.phi1(map.phi1(dN)) ; // VEC3 p = position[dN]; map.collapseEdge(dN) ; @@ -548,7 +596,8 @@ void simplifyFreeSpace(typename PFP::MAP& map, typename PFP::TVEC3& position, } // simplify vertices of valence 2 -> check if no intersection is created - if (map.alpha1(map.alpha1(dN)) == dN && !obstacleMark.isMarked(dN)) { + if (map.alpha1(map.alpha1(dN)) == dN && !obstacleMark.isMarked(dN)) + { if (dN == dd || dN == map.phi2(dd)) dd = map.phi1(dd) ; VEC3 p = position[map.phi1(dN)] ; @@ -556,42 +605,46 @@ void simplifyFreeSpace(typename PFP::MAP& map, typename PFP::TVEC3& position, Dart dI = map.phi1(map.phi1(dN)) ; VEC3 pA = position[map.phi_1(dN)] ; VEC3 pB = position[map.phi1(dN)] ; - do { + do + { VEC3 p1 = position[dI] ; VEC3 p2 = position[map.phi1(dI)] ; if (Geom::testOrientation2D(p1, pA, pB) - != Geom::testOrientation2D(p2, pA, pB) - && Geom::testOrientation2D(pA, p1, p2) - != Geom::testOrientation2D(pB, p1, p2)) { - if ( !Geom::arePointsEquals(p1, pA) - && !Geom::arePointsEquals(p1, pB) - && !Geom::arePointsEquals(p2, pA) - && !Geom::arePointsEquals(p2, pB)) intersect = true ; + != Geom::testOrientation2D(p2, pA, pB) + && Geom::testOrientation2D(pA, p1, p2) + != Geom::testOrientation2D(pB, p1, p2)) + { + if (!Geom::arePointsEquals(p1, pA) && !Geom::arePointsEquals(p1, pB) + && !Geom::arePointsEquals(p2, pA) + && !Geom::arePointsEquals(p2, pB)) intersect = true ; } dI = map.phi1(dI) ; } while (dI != map.phi_1(dN)) ; - if ( !intersect) { + if (!intersect) + { dN = map.phi2(dN) ; Dart dI = map.phi1(map.phi1(dN)) ; - do { + do + { VEC3 v ; VEC3 p1 = position[dI] ; VEC3 p2 = position[map.phi1(dI)] ; if (Geom::testOrientation2D(p1, pA, pB) - != Geom::testOrientation2D(p2, pA, pB) - && Geom::testOrientation2D(pA, p1, p2) - != Geom::testOrientation2D(pB, p1, p2)) { - if ( !Geom::arePointsEquals(p1, pA) - && !Geom::arePointsEquals(p1, pB) - && !Geom::arePointsEquals(p2, pA) - && !Geom::arePointsEquals(p2, pB)) intersect = true ; + != Geom::testOrientation2D(p2, pA, pB) + && Geom::testOrientation2D(pA, p1, p2) + != Geom::testOrientation2D(pB, p1, p2)) + { + if (!Geom::arePointsEquals(p1, pA) && !Geom::arePointsEquals(p1, pB) + && !Geom::arePointsEquals(p2, pA) + && !Geom::arePointsEquals(p2, pB)) intersect = true ; } dI = map.phi1(dI) ; } while (dI != map.phi_1(dN)) ; } - if ( !intersect) { + if (!intersect) + { map.collapseEdge(dN) ; position[map.phi1(ddd)] = p ; } @@ -601,18 +654,21 @@ void simplifyFreeSpace(typename PFP::MAP& map, typename PFP::TVEC3& position, } while (ddd != dd) ; ddd = dd ; - do { - if (eM.isMarked(ddd)) { + do + { + if (eM.isMarked(ddd)) + { eM.unmark(ddd) ; tableIt[ddd] = orderedD.insert( - std::make_pair(tableArea[ddd] + tableArea[map.phi2(ddd)], ddd)) ; + std::make_pair(tableArea[ddd] + tableArea[map.phi2(ddd)], ddd)) ; } ddd = map.phi1(ddd) ; } while (ddd != dd) ; it = orderedD.begin() ; } - else { + else + { ++it ; } @@ -621,13 +677,16 @@ void simplifyFreeSpace(typename PFP::MAP& map, typename PFP::TVEC3& position, convexifyFreeSpace(map, position, obstacleMark, buildingMark) ; - for (Dart d = map.begin() ; d != map.end() ; map.next(d)) { - if (buildingMark.isMarked(d) && !buildingMark.isMarked(map.phi2(d))) { + for (Dart d = map.begin(); d != map.end(); map.next(d)) + { + if (buildingMark.isMarked(d) && !buildingMark.isMarked(map.phi2(d))) + { if (map.alpha1(map.alpha1(map.phi1(d))) == map.phi1(d) - && Geom::testOrientation2D(position[d], position[map.phi1(d)], - position[map.phi1(map.phi1(d))]) == Geom::ALIGNED) + && Geom::testOrientation2D(position[d], position[map.phi1(d)], + position[map.phi1(map.phi1(d))]) + == Geom::ALIGNED) - { + { VEC3 p = position[map.phi1(map.phi1(d))] ; // Dart dN = map.phi1(d); map.collapseEdge(map.phi1(d)) ; @@ -639,34 +698,36 @@ void simplifyFreeSpace(typename PFP::MAP& map, typename PFP::TVEC3& position, map.check() ; } -template +template bool isAnEar(typename PFP::MAP& map, typename PFP::TVEC3& position, Dart dd, float& area) { typedef typename PFP::VEC3 VEC3 ; bool check = false ; if (Geom::testOrientation2D(position[dd], position[map.phi_1(dd)], position[map.phi1(dd)]) - == Geom::RIGHT && !map.sameFace(map.phi2(dd), map.phi2(map.phi_1(dd)))) { + == Geom::RIGHT + && !map.sameFace(map.phi2(dd), map.phi2(map.phi_1(dd)))) + { VEC3 p = position[dd] ; VEC3 p1 = position[map.phi1(dd)] ; VEC3 p_1 = position[map.phi_1(dd)] ; Dart ddd = map.phi1(map.phi1(dd)) ; check = true ; - do { + do + { unsigned int incl = isPointInTriangle(position[ddd], p_1, p, p1) ; - if ((incl == Geom::FACE_INCLUSION) || (incl == Geom::EDGE_INCLUSION)) check = - false ; + if ((incl == Geom::FACE_INCLUSION) || (incl == Geom::EDGE_INCLUSION)) check = false ; ddd = map.phi1(ddd) ; } while (check && ddd != map.phi_1(dd)) ; - if (check) { + if (check) + { VEC3 v1(p1 - p) ; VEC3 v2(p - p_1) ; VEC3 v3(p_1 - p1) ; area = 1.0f - / std::min( - std::min(Geom::angle(v1, -1.0f * v2), Geom::angle( -1.0f * v1, v3)), - Geom::angle(v2, -1.0f * v3)) ; + / std::min(std::min(Geom::angle(v1, -1.0f * v2), Geom::angle(-1.0f * v1, v3)), + Geom::angle(v2, -1.0f * v3)) ; // area = 1.0f / Geom::triangleArea(p_1, p, p1); } } @@ -674,13 +735,14 @@ bool isAnEar(typename PFP::MAP& map, typename PFP::TVEC3& position, Dart dd, flo return check ; } -template +template bool canRemoveEdgeConvex(typename PFP::MAP& map, typename PFP::TVEC3& position, Dart d, - CellMarker& obstacleMark) + CellMarker& obstacleMark) { typedef typename PFP::VEC3 VEC3 ; - if ( !obstacleMark.isMarked(d) && position[d][2] == 0.0f) { + if (!obstacleMark.isMarked(d) && position[d][2] == 0.0f) + { Dart d2 = map.phi2(d) ; VEC3 p1 = position[map.phi_1(d)] ; VEC3 p2 = position[d] ; @@ -691,53 +753,59 @@ bool canRemoveEdgeConvex(typename PFP::MAP& map, typename PFP::TVEC3& position, VEC3 p5 = position[map.phi1(map.phi1(d))] ; VEC3 p6 = position[map.phi1(map.phi1(d2))] ; if ((p1[0] != p6[0] || p1[1] != p6[1] || p1[2] != p6[2]) - && (p5[0] != p4[0] || p5[1] != p4[1] || p5[2] != p4[2]) - && Geom::testOrientation2D(p6, p1, p2) != Geom::RIGHT - && Geom::testOrientation2D(p5, p4, p3) != Geom::RIGHT) return true ; + && (p5[0] != p4[0] || p5[1] != p4[1] || p5[2] != p4[2]) + && Geom::testOrientation2D(p6, p1, p2) != Geom::RIGHT + && Geom::testOrientation2D(p5, p4, p3) != Geom::RIGHT) return true ; } return false ; } -template +template void convexifyFreeSpace(typename PFP::MAP& map, typename PFP::TVEC3& position, - CellMarker& obstacleMark, CellMarker& buildingMark) + CellMarker& obstacleMark, CellMarker& buildingMark) { typedef typename PFP::VEC3 VEC3 ; float area ; CellMarker m(map, FACE) ; - for (Dart d = map.begin() ; d != map.end() ; map.next(d)) { - if ( !m.isMarked(d) && !obstacleMark.isMarked(d) && !buildingMark.isMarked(d)) { + for (Dart d = map.begin(); d != map.end(); map.next(d)) + { + if (!m.isMarked(d) && !obstacleMark.isMarked(d) && !buildingMark.isMarked(d)) + { std::multimap moultipass ; std::map::iterator> liloo ; // store ears in multimap Dart dd = d ; - do { - if (isAnEar(map, position, dd, area)) - liloo.insert( - std::pair::iterator>(dd, - moultipass.insert(std::pair(area, dd)))) ; + do + { + if (isAnEar(map, position, dd, area)) liloo.insert( + std::pair::iterator>( + dd, moultipass.insert(std::pair(area, dd)))) ; dd = map.phi1(dd) ; } while (dd != d) ; - while (map.faceDegree(moultipass.begin()->second) > 3) { + while (map.faceDegree(moultipass.begin()->second) > 3) + { Dart dd = moultipass.begin()->second ; - if (liloo.find(map.phi_1(dd)) != liloo.end()) { + if (liloo.find(map.phi_1(dd)) != liloo.end()) + { moultipass.erase(liloo[map.phi_1(dd)]) ; liloo.erase(map.phi_1(dd)) ; } - if (moultipass.size() > 0) { + if (moultipass.size() > 0) + { moultipass.erase(liloo[dd]) ; liloo.erase(dd) ; } - if (liloo.find(map.phi1(dd)) != liloo.end()) { + if (liloo.find(map.phi1(dd)) != liloo.end()) + { moultipass.erase(liloo[map.phi1(dd)]) ; liloo.erase(map.phi1(dd)) ; } @@ -745,16 +813,14 @@ void convexifyFreeSpace(typename PFP::MAP& map, typename PFP::TVEC3& position, map.splitFace(map.phi_1(dd), map.phi1(dd)) ; Dart dNext = map.alpha_1(map.phi1(dd)) ; - if (isAnEar(map, position, dNext, area)) - liloo.insert( - std::pair::iterator>(dNext, - moultipass.insert(std::pair(area, dNext)))) ; + if (isAnEar(map, position, dNext, area)) liloo.insert( + std::pair::iterator>( + dNext, moultipass.insert(std::pair(area, dNext)))) ; Dart dPrev = map.alpha1(map.phi_1(dd)) ; - if (isAnEar(map, position, dPrev, area)) - liloo.insert( - std::pair::iterator>(dPrev, - moultipass.insert(std::pair(area, dPrev)))) ; + if (isAnEar(map, position, dPrev, area)) liloo.insert( + std::pair::iterator>( + dPrev, moultipass.insert(std::pair(area, dPrev)))) ; m.mark(dd) ; } @@ -768,16 +834,19 @@ void convexifyFreeSpace(typename PFP::MAP& map, typename PFP::TVEC3& position, // map.mergeFaces(d); } -template +template void installGuardRail(typename PFP::MAP& map, typename PFP::TVEC3& position, - CellMarker& obstacleMark, CellMarker& buildingMark, float height) + CellMarker& obstacleMark, CellMarker& buildingMark, float height) { CellMarker cm(map, EDGE) ; CellMarkerNoUnmark railVert(map, EDGE) ; - for (Dart d = map.begin() ; d != map.end() ; map.next(d)) { - if ( !cm.isMarked(d)) { + for (Dart d = map.begin(); d != map.end(); map.next(d)) + { + if (!cm.isMarked(d)) + { cm.mark(d) ; - if (obstacleMark.isMarked(d)) { + if (obstacleMark.isMarked(d)) + { Dart dExt = map.newFace(4) ; Dart dIn = map.newFace(4) ; @@ -793,7 +862,8 @@ void installGuardRail(typename PFP::MAP& map, typename PFP::TVEC3& position, Dart ddExt = dExt ; Dart ddIn = dIn ; - do { + do + { cm.mark(ddExt) ; cm.mark(ddIn) ; ddExt = map.phi1(ddExt) ; @@ -817,8 +887,10 @@ void installGuardRail(typename PFP::MAP& map, typename PFP::TVEC3& position, } } - for (Dart d = map.begin() ; d != map.end() ; map.next(d)) { - if (railVert.isMarked(d)) { + for (Dart d = map.begin(); d != map.end(); map.next(d)) + { + if (railVert.isMarked(d)) + { railVert.unmark(d) ; map.sewFaces(d, map.phi_1(map.alpha_1(d))) ; } diff --git a/include/env_map.h b/include/env_map.h index 9b07ae31e7b86f60f3c0c64458c321f8d8a96a99..6057fd204f849f3f6ebf0f6f0868920f71debd06 100644 --- a/include/env_map.h +++ b/include/env_map.h @@ -127,8 +127,8 @@ public: CellMarker pedWayMark ; #ifndef SPATIAL_HASHING - static const unsigned int nbAgentsToSubdivide = 4 ; - static const unsigned int nbAgentsToSimplify = 1 ; + static const unsigned int nbAgentsToSubdivide = 10 ; + static const unsigned int nbAgentsToSimplify = 4 ; CellMarker refineMark ; CellMarker coarsenMark ; @@ -139,20 +139,27 @@ public: #endif #ifdef SPATIAL_HASHING - float a_cell_nb_x, a_cell_nb_y ; - float o_cell_nb_x, o_cell_nb_y ; + unsigned int agentGridSize(unsigned int i) + { + return geometry.size(i) / minCellSize ; + } + + unsigned int obstacleGridSize(unsigned int i) + { + return geometry.size(i) / obstacleDistance ; + } Geom::Vec2ui agentPositionCell(VEC3& pos) { - VEC3 relativePos = pos - geometry.min(); - relativePos /= minCellSize; + VEC3 relativePos = pos - geometry.min() ; + relativePos /= minCellSize ; return Geom::Vec2ui(relativePos[0], relativePos[1]) ; } Geom::Vec2ui obstaclePositionCell(VEC3& pos) { - VEC3 relativePos = pos - geometry.min(); - relativePos /= obstacleDistance; + VEC3 relativePos = pos - geometry.min() ; + relativePos /= obstacleDistance ; return Geom::Vec2ui(relativePos[0], relativePos[1]) ; } @@ -166,12 +173,14 @@ public: * INLINE FUNCTIONS * **************************************/ -template +template inline void removeElementFromVector(std::vector& a, T ag) { typename std::vector::iterator end = a.template end() ; - for (typename std::vector::iterator it = a.begin() ; it != end ; ++it) { - if ( *it == ag) { + for (typename std::vector::iterator it = a.begin(); it != end; ++it) + { + if (*it == ag) + { *it = a.back() ; a.pop_back() ; return ; @@ -180,7 +189,8 @@ inline void removeElementFromVector(std::vector& a, T ag) } #ifndef SPATIAL_HASHING -inline unsigned int EnvMap::totalNeighborSize(Dart d) { +inline unsigned int EnvMap::totalNeighborSize(Dart d) +{ return agentvect[d].size() + neighborAgentvect[d].size() ; } @@ -190,7 +200,8 @@ inline void EnvMap::nbAgentsIncrease(Dart d) if (totalNeighborSize(d) < nbAgentsToSubdivide) return ; std::pair& sf = subdivisableFace[d] ; - if (sf.first == false || (sf.first == true && sf.second)) { + if (sf.first == false || (sf.first == true && sf.second)) + { refineMark.mark(d) ; refineCandidate.push_back(d) ; } @@ -213,9 +224,11 @@ inline void EnvMap::pushAgentInCells(Agent* agent, Dart d) // nbAgentsIncrease(d); Dart dd = d ; - do { + do + { Dart ddd = map.alpha1(map.alpha1(dd)) ; - while (ddd != dd) { + while (ddd != dd) + { neighborAgentvect[ddd].push_back(agent) ; // nbAgentsIncrease(ddd); ddd = map.alpha1(ddd) ; @@ -232,9 +245,11 @@ inline void EnvMap::popAgentInCells(Agent* agent, Dart d) // nbAgentsDecrease(d) ; Dart dd = d ; - do { + do + { Dart ddd = map.alpha1(map.alpha1(dd)) ; - while (ddd != dd) { + while (ddd != dd) + { removeElementFromVector(neighborAgentvect[ddd], agent) ; // nbAgentsDecrease(ddd) ; ddd = map.alpha1(ddd) ; @@ -246,7 +261,7 @@ inline void EnvMap::popAgentInCells(Agent* agent, Dart d) inline void EnvMap::pushObstacleInCells(Obstacle* o, Dart d) { assert(map.getCurrentLevel() == map.getMaxLevel()) ; - assert( !buildingMark.isMarked(d)) ; + assert(!buildingMark.isMarked(d)) ; obstvect[d].push_back(o) ; @@ -270,7 +285,7 @@ inline void EnvMap::pushObstacleInCells(Obstacle* o, Dart d) inline void EnvMap::popObstacleInCells(Obstacle* o, Dart d) { assert(map.getCurrentLevel() == map.getMaxLevel()) ; - assert( !buildingMark.isMarked(d)) ; + assert(!buildingMark.isMarked(d)) ; removeElementFromVector(obstvect[d], o) ; diff --git a/include/env_render.h b/include/env_render.h index bb585db3847b50643a0c794139acc2cd79d7b2b4..049900c74bb0380dd83f30fc17662bc1690be488 100644 --- a/include/env_render.h +++ b/include/env_render.h @@ -7,70 +7,72 @@ inline void renderDart(EnvMap& m, Dart d) { - PFP::VEC3 p1 = m.position[d]; - PFP::VEC3 p2 = m.position[m.map.phi1(d)]; + PFP::VEC3 p1 = m.position[d] ; + PFP::VEC3 p2 = m.position[m.map.phi1(d)] ; - glBegin(GL_LINES); - glVertex3f(p1[0],p1[1],p1[2]); - glVertex3f(p2[0],p2[1],p2[2]); - glEnd(); + glBegin(GL_LINES) ; + glVertex3f(p1[0], p1[1], p1[2]) ; + glVertex3f(p2[0], p2[1], p2[2]) ; + glEnd() ; } inline void renderFace(EnvMap& m, Dart d) { - Dart dd=d; - do { - renderDart(m,dd); - dd = m.map.phi1(dd); - }while(dd!=d); + Dart dd = d ; + do + { + renderDart(m, dd) ; + dd = m.map.phi1(dd) ; + } while (dd != d) ; } inline void renderPredictionTriangle(EnvMap& m, Dart d, PFP::VEC3 p) { - Geom::Plane3D pl = Algo::Geometry::facePlane(m.map,d,m.position); - p[2] -= 1000; - Geom::intersectionPlaneRay(pl,p,VEC3(0,0,-1),p); - - VEC3 pos1(m.position[d]); - VEC3 pos2(m.position[m.map.phi1(d)]); - pos2[2] += 2; - glBegin(GL_LINE_LOOP); - glVertex3fv(&p[0]); - glVertex3fv(& pos1[0]); - glVertex3fv(& pos2[0]); - glEnd(); + Geom::Plane3D pl = Algo::Geometry::facePlane(m.map, d, m.position) ; + p[2] -= 1000 ; + Geom::intersectionPlaneRay(pl, p, VEC3(0, 0, -1), p) ; + + VEC3 pos1(m.position[d]) ; + VEC3 pos2(m.position[m.map.phi1(d)]) ; + pos2[2] += 2 ; + glBegin(GL_LINE_LOOP) ; + glVertex3fv(&p[0]) ; + glVertex3fv(&pos1[0]) ; + glVertex3fv(&pos2[0]) ; + glEnd() ; } -static const float cosT[5] = { 1, 0.309017, -0.809017, -0.809017, 0.309017 }; -static const float sinT[5] = { 0, 0.951057, 0.587785, -0.587785, -0.951057 }; +static const float cosT[5] = { 1, 0.309017, -0.809017, -0.809017, 0.309017 } ; +static const float sinT[5] = { 0, 0.951057, 0.587785, -0.587785, -0.951057 } ; -inline void renderAgent(EnvMap& m, Agent* agent, bool showNeighborDist = false, bool showObstacleDist = false) +inline void renderAgent(EnvMap& m, Agent* agent, bool showNeighborDist = false, + bool showObstacleDist = false) { #ifdef SPATIAL_HASHING - const VEC3& pos = agent->pos; + const VEC3& pos = agent->pos ; #else - VEC3 pos = agent->part_.m_position; + VEC3 pos = agent->part_.m_position ; #endif - float radius = agent->radius_; - + float radius = agent->radius_ ; + // Geom::Plane3D pl = Algo::Geometry::facePlane(m.map,agent->part_.d,m.position); // pos[2] -= 1000; // Geom::intersectionPlaneRay(pl,pos,VEC3(0,0,-1),pos); - glLineWidth(1.0f); - + glLineWidth(1.0f) ; + // VEC3 col = Utils::color_map_BCGYR(float(agent->agentNo)/float(agent->sim_->agents_.size())); // glColor3fv(col.data()); - glColor3f(1.0f, 0.0f, 0.0f); - glBegin(GL_POLYGON); - for(unsigned int i = 0; i < 5; ++i) - glVertex3f(pos[0] + (cosT[i] * radius), pos[1] + (sinT[i] * radius), pos[2]+0.01f); - glEnd(); - - VEC3 dir = agent->velocity_; - dir.normalize(); - - //draw the speed vector + glColor3f(1.0f, 0.0f, 0.0f) ; + glBegin(GL_POLYGON) ; + for (unsigned int i = 0; i < 5; ++i) + glVertex3f(pos[0] + (cosT[i] * radius), pos[1] + (sinT[i] * radius), pos[2] + 0.01f) ; + glEnd() ; + + VEC3 dir = agent->velocity_ ; + dir.normalize() ; + + //draw the speed vector // VEC3 base(0,0,1); // VEC3 myAxisRot = base^dir; // myAxisRot.normalize(); @@ -88,37 +90,38 @@ inline void renderAgent(EnvMap& m, Agent* agent, bool showNeighborDist = false, // glEnd(); // glPopMatrix(); - //show goals - if(true) +//show goals + if (true) { - glLineWidth(3.0f); - glBegin(GL_LINE_STRIP); - for(std::vector::iterator it = ++(agent->goals_.begin()) ; it != agent->goals_.end() ; ++it) + glLineWidth(3.0f) ; + glBegin(GL_LINE_STRIP) ; + for (std::vector::iterator it = ++(agent->goals_.begin()); it != agent->goals_.end(); + ++it) { - glVertex3f((*it)[0],(*it)[1],(*it)[2]); + glVertex3f((*it)[0], (*it)[1], (*it)[2]) ; } - glEnd(); - glLineWidth(1.0f); + glEnd() ; + glLineWidth(1.0f) ; } - if(showNeighborDist) + if (showNeighborDist) { - radius = agent->neighborDist_; - glColor3f(0.0f,1.0f,0.0f); - glBegin(GL_LINE_LOOP); - for(unsigned int i = 0; i < 5; ++i) - glVertex3f(pos[0] + (cosT[i] * radius), pos[1] + (sinT[i] * radius), pos[2]+0.01f); - glEnd(); + radius = agent->neighborDist_ ; + glColor3f(0.0f, 1.0f, 0.0f) ; + glBegin(GL_LINE_LOOP) ; + for (unsigned int i = 0; i < 5; ++i) + glVertex3f(pos[0] + (cosT[i] * radius), pos[1] + (sinT[i] * radius), pos[2] + 0.01f) ; + glEnd() ; } - - if(showObstacleDist) + + if (showObstacleDist) { - radius = (agent->timeHorizonObst_ * agent->maxSpeed_) + agent->radius_; - glColor3f(0.0f,0.0f,1.0f); - glBegin(GL_LINE_LOOP); - for(unsigned int i = 0; i < 5; ++i) - glVertex3f(pos[0] + (cosT[i] * radius), pos[1] + (sinT[i] * radius), pos[2]+0.01f); - glEnd(); + radius = (agent->timeHorizonObst_ * agent->maxSpeed_) + agent->radius_ ; + glColor3f(0.0f, 0.0f, 1.0f) ; + glBegin(GL_LINE_LOOP) ; + for (unsigned int i = 0; i < 5; ++i) + glVertex3f(pos[0] + (cosT[i] * radius), pos[1] + (sinT[i] * radius), pos[2] + 0.01f) ; + glEnd() ; } } diff --git a/include/exportObstacles.h b/include/exportObstacles.h index e720032d3e9dfcd058c76dc2615c849487f044b2..7b67adcac0c61d169267c0cf44a0301b757b248f 100644 --- a/include/exportObstacles.h +++ b/include/exportObstacles.h @@ -7,8 +7,9 @@ namespace CGoGN namespace ExportScene { - template - bool exportSceneToFile(typename PFP::MAP& map,const typename PFP::TVEC3& position, CellMarker& obstacleMark, CellMarker& buildingMark, std::string& filename); +template +bool exportSceneToFile(typename PFP::MAP& map, const typename PFP::TVEC3& position, + CellMarker& obstacleMark, CellMarker& buildingMark, std::string& filename) ; } diff --git a/include/exportObstacles.hpp b/include/exportObstacles.hpp index b6e1ab85d57fe742da679964abfa09eb87a78c92..7dcd6113a1f468f3579dada09ee932700ce1099e 100644 --- a/include/exportObstacles.hpp +++ b/include/exportObstacles.hpp @@ -7,34 +7,35 @@ namespace ExportScene { template -bool exportSceneToFile(typename PFP::MAP& map,const typename PFP::TVEC3& position, CellMarker& obstacleMark, CellMarker& buildingMark, std::string& filename) +bool exportSceneToFile(typename PFP::MAP& map, const typename PFP::TVEC3& position, + CellMarker& obstacleMark, CellMarker& buildingMark, std::string& filename) { - std::ofstream out(filename.c_str(), std::ios::out); + std::ofstream out(filename.c_str(), std::ios::out) ; if (!out.good()) { - std::cerr << "(export) Unable to open file " << filename << std::endl; - return false; + std::cerr << "(export) Unable to open file " << filename << std::endl ; + return false ; } - CellMarker f(map, FACE); + CellMarker f(map, FACE) ; - for(Dart d = map.begin(); d != map.end(); map.next(d)) + for (Dart d = map.begin(); d != map.end(); map.next(d)) { - if(buildingMark.isMarked(d) && !f.isMarked(d)) + if (buildingMark.isMarked(d) && !f.isMarked(d)) { - f.mark(d); - Dart dd = d; + f.mark(d) ; + Dart dd = d ; do { - out << position[dd] << std::endl; - dd = map.phi1(dd); - } while(dd != d); - out << std::endl; + out << position[dd] << std::endl ; + dd = map.phi1(dd) ; + } while (dd != d) ; + out << std::endl ; } } - out.close(); - return true; + out.close() ; + return true ; } } diff --git a/include/gl2ps.h b/include/gl2ps.h index 9dbb726b41a08d4fdbf6efc3410f7cfda2c15020..2a6c1756354637d908cc4985c4c107b6b87ea557 100644 --- a/include/gl2ps.h +++ b/include/gl2ps.h @@ -67,7 +67,7 @@ #endif /* Support for compressed PostScript/PDF/SVG and for embedded PNG - images in SVG */ + images in SVG */ #if defined(HAVE_ZLIB) || defined(HAVE_LIBZ) # define GL2PS_HAVE_ZLIB @@ -139,10 +139,10 @@ #define GL2PS_BLEND 4 /* Text alignment (o=raster position; default mode is BL): - +---+ +---+ +---+ +---+ +---+ +---+ +-o-+ o---+ +---o - | o | o | | o | | | | | | | | | | | | - +---+ +---+ +---+ +-o-+ o---+ +---o +---+ +---+ +---+ - C CL CR B BL BR T TL TR */ + +---+ +---+ +---+ +---+ +---+ +---+ +-o-+ o---+ +---o + | o | o | | o | | | | | | | | | | | | + +---+ +---+ +---+ +-o-+ o---+ +---o +---+ +---+ +---+ + C CL CR B BL BR T TL TR */ #define GL2PS_TEXT_C 1 #define GL2PS_TEXT_CL 2 @@ -154,43 +154,39 @@ #define GL2PS_TEXT_TL 8 #define GL2PS_TEXT_TR 9 -typedef GLfloat GL2PSrgba[4]; +typedef GLfloat GL2PSrgba[4] ; #if defined(__cplusplus) -extern "C" { +extern "C" +{ #endif -GL2PSDLL_API GLint gl2psBeginPage(const char *title, const char *producer, - GLint viewport[4], GLint format, GLint sort, - GLint options, GLint colormode, - GLint colorsize, GL2PSrgba *colormap, - GLint nr, GLint ng, GLint nb, GLint buffersize, - FILE *stream, const char *filename); -GL2PSDLL_API GLint gl2psEndPage(void); -GL2PSDLL_API GLint gl2psSetOptions(GLint options); -GL2PSDLL_API GLint gl2psGetOptions(GLint *options); -GL2PSDLL_API GLint gl2psBeginViewport(GLint viewport[4]); -GL2PSDLL_API GLint gl2psEndViewport(void); -GL2PSDLL_API GLint gl2psText(const char *str, const char *fontname, - GLshort fontsize); -GL2PSDLL_API GLint gl2psTextOpt(const char *str, const char *fontname, - GLshort fontsize, GLint align, GLfloat angle); -GL2PSDLL_API GLint gl2psSpecial(GLint format, const char *str); -GL2PSDLL_API GLint gl2psDrawPixels(GLsizei width, GLsizei height, - GLint xorig, GLint yorig, - GLenum format, GLenum type, const void *pixels); -GL2PSDLL_API GLint gl2psEnable(GLint mode); -GL2PSDLL_API GLint gl2psDisable(GLint mode); -GL2PSDLL_API GLint gl2psPointSize(GLfloat value); -GL2PSDLL_API GLint gl2psLineWidth(GLfloat value); -GL2PSDLL_API GLint gl2psBlendFunc(GLenum sfactor, GLenum dfactor); +GL2PSDLL_API GLint gl2psBeginPage(const char *title, const char *producer, GLint viewport[4], + GLint format, GLint sort, GLint options, GLint colormode, + GLint colorsize, GL2PSrgba *colormap, GLint nr, GLint ng, + GLint nb, GLint buffersize, FILE *stream, const char *filename) ; +GL2PSDLL_API GLint gl2psEndPage(void) ; +GL2PSDLL_API GLint gl2psSetOptions(GLint options) ; +GL2PSDLL_API GLint gl2psGetOptions(GLint *options) ; +GL2PSDLL_API GLint gl2psBeginViewport(GLint viewport[4]) ; +GL2PSDLL_API GLint gl2psEndViewport(void) ; +GL2PSDLL_API GLint gl2psText(const char *str, const char *fontname, GLshort fontsize) ; +GL2PSDLL_API GLint gl2psTextOpt(const char *str, const char *fontname, GLshort fontsize, + GLint align, GLfloat angle) ; +GL2PSDLL_API GLint gl2psSpecial(GLint format, const char *str) ; +GL2PSDLL_API GLint gl2psDrawPixels(GLsizei width, GLsizei height, GLint xorig, GLint yorig, + GLenum format, GLenum type, const void *pixels) ; +GL2PSDLL_API GLint gl2psEnable(GLint mode) ; +GL2PSDLL_API GLint gl2psDisable(GLint mode) ; +GL2PSDLL_API GLint gl2psPointSize(GLfloat value) ; +GL2PSDLL_API GLint gl2psLineWidth(GLfloat value) ; +GL2PSDLL_API GLint gl2psBlendFunc(GLenum sfactor, GLenum dfactor) ; /* undocumented */ -GL2PSDLL_API GLint gl2psDrawImageMap(GLsizei width, GLsizei height, - const GLfloat position[3], - const unsigned char *imagemap); -GL2PSDLL_API const char *gl2psGetFileExtension(GLint format); -GL2PSDLL_API const char *gl2psGetFormatDescription(GLint format); +GL2PSDLL_API GLint gl2psDrawImageMap(GLsizei width, GLsizei height, const GLfloat position[3], + const unsigned char *imagemap) ; +GL2PSDLL_API const char *gl2psGetFileExtension(GLint format) ; +GL2PSDLL_API const char *gl2psGetFormatDescription(GLint format) ; #if defined(__cplusplus) } diff --git a/include/obstacle.h b/include/obstacle.h index ee4d20ff4668752ee68fae55fd317fac06ede0bc..e502f25a19ceed04dfa9e0819386e123e7a33a75 100644 --- a/include/obstacle.h +++ b/include/obstacle.h @@ -19,6 +19,6 @@ public: VEC3 p2 ; VEC3 prevP ; VEC3 nextP ; -}; +} ; #endif diff --git a/include/path_finder.h b/include/path_finder.h index 9a5318d1f26d4e878e104d7ec8fbe07293399997..87768a3e02f69e2b260d7a610eab8d9cb7e81204 100644 --- a/include/path_finder.h +++ b/include/path_finder.h @@ -7,11 +7,13 @@ namespace CGoGN namespace PathFinder { - template - float pathCostSqr(typename PFP::MAP& map, const typename PFP::TVEC3& position, Dart startPos, Dart stopPos, CellMarker& bad); +template +float pathCostSqr(typename PFP::MAP& map, const typename PFP::TVEC3& position, Dart startPos, + Dart stopPos, CellMarker& bad) ; - template - std::vector pathFindAStar(typename PFP::MAP& map, const typename PFP::TVEC3& position, Dart startPos, Dart stopPos, CellMarker& bad); +template +std::vector pathFindAStar(typename PFP::MAP& map, const typename PFP::TVEC3& position, + Dart startPos, Dart stopPos, CellMarker& bad) ; } diff --git a/include/path_finder.hpp b/include/path_finder.hpp index f2c9dec4e28bc7d917e51c859bd5346492425f81..53c2dbeba8a145c2d4c11e48a850aa8d0e48fae8 100644 --- a/include/path_finder.hpp +++ b/include/path_finder.hpp @@ -8,9 +8,9 @@ namespace CGoGN namespace PathFinder { - template -float pathCostSqr(typename PFP::MAP& map,const typename PFP::TVEC3& position, Dart startPos, Dart stopPos, CellMarker& bad) +float pathCostSqr(typename PFP::MAP& map, const typename PFP::TVEC3& position, Dart startPos, + Dart stopPos, CellMarker& bad) { // float penalty=100000000.0f; // Dart dd = startPos; @@ -23,85 +23,90 @@ float pathCostSqr(typename PFP::MAP& map,const typename PFP::TVEC3& position, Da // dd = map.phi1(dd); // } while(dd!=startPos); - return VEC3(Algo::Geometry::faceCentroid(map,stopPos,position)-Algo::Geometry::faceCentroid(map,startPos,position)).norm2(); + return VEC3( + Algo::Geometry::faceCentroid(map, stopPos, position) + - Algo::Geometry::faceCentroid(map, startPos, position)).norm2() ; // +penalty // +(position[stopPos][2]-position[startPos][2])*1000.0f; } template -std::vector pathFindAStar(typename PFP::MAP& map,const typename PFP::TVEC3& position, Dart startPos, Dart stopPos, CellMarker& bad) +std::vector pathFindAStar(typename PFP::MAP& map, const typename PFP::TVEC3& position, + Dart startPos, Dart stopPos, CellMarker& bad) { - std::vector path; + std::vector path ; - AutoAttributeHandler< float > tablePathCost(map,FACE); - AutoAttributeHandler< Dart > tablePred(map,FACE); + AutoAttributeHandler tablePathCost(map, FACE) ; + AutoAttributeHandler tablePred(map, FACE) ; - std::multimap vToDev; + std::multimap vToDev ; - CellMarker m(map,FACE); + CellMarker m(map, FACE) ; - vToDev.insert(std::make_pair(0, stopPos)); + vToDev.insert(std::make_pair(0, stopPos)) ; - m.mark(stopPos); + m.mark(stopPos) ; - tablePred[stopPos] = stopPos; - tablePathCost[stopPos] = 0; + tablePred[stopPos] = stopPos ; + tablePathCost[stopPos] = 0 ; - Dart toDev; + Dart toDev ; do { //get all vertex-adjacent cells - toDev = (vToDev.begin())->second; - vToDev.erase(vToDev.begin()); + toDev = (vToDev.begin())->second ; + vToDev.erase(vToDev.begin()) ; - Dart dd = toDev; + Dart dd = toDev ; do { // Dart ddd = map.phi2(dd); - Dart ddd = map.alpha1(map.alpha1(dd)); - while(ddd != dd) + Dart ddd = map.alpha1(map.alpha1(dd)) ; + while (ddd != dd) { //evaluate their cost and push them in the vector to dev - if(!m.isMarked(ddd)) + if (!m.isMarked(ddd)) { - m.mark(ddd); - if(!bad.isMarked(ddd)) + m.mark(ddd) ; + if (!bad.isMarked(ddd)) { - if(tablePred[ddd]==EMBNULL) + if (tablePred[ddd] == EMBNULL) { - tablePred[ddd]=dd; + tablePred[ddd] = dd ; // tablePred[ddd]=toDev; - tablePathCost[ddd] = tablePathCost[toDev]+pathCostSqr(map,position,toDev,ddd,bad); - float costDDD=pathCostSqr(map,position,startPos,ddd,bad)+tablePathCost[toDev]; - vToDev.insert(std::make_pair(costDDD, ddd)); + tablePathCost[ddd] = tablePathCost[toDev] + + pathCostSqr(map, position, toDev, ddd, bad) ; + float costDDD = pathCostSqr(map, position, startPos, ddd, bad) + + tablePathCost[toDev] ; + vToDev.insert(std::make_pair(costDDD, ddd)) ; } else - std::cout << "path_planning : missing case" << std::endl; + std::cout << "path_planning : missing case" << std::endl ; } } - ddd = map.alpha1(ddd); + ddd = map.alpha1(ddd) ; } - dd = map.phi1(dd); - } while(dd != toDev); + dd = map.phi1(dd) ; + } while (dd != toDev) ; - } while(vToDev.size()>0 && !map.sameFace(startPos,toDev)); + } while (vToDev.size() > 0 && !map.sameFace(startPos, toDev)) ; //if path found : from start to stop -> push all predecessors - if(vToDev.size()>0 && map.sameFace(startPos,toDev)) + if (vToDev.size() > 0 && map.sameFace(startPos, toDev)) { - Dart toPush=startPos; + Dart toPush = startPos ; do { - path.push_back(toPush); - toPush = tablePred[toPush]; - } while(!map.sameFace(toPush,stopPos)); + path.push_back(toPush) ; + toPush = tablePred[toPush] ; + } while (!map.sameFace(toPush, stopPos)) ; } else - std::cout << "fichtre" << std::endl; + std::cout << "fichtre" << std::endl ; - return path; + return path ; } //namespace diff --git a/include/simulator.h b/include/simulator.h index ec19114ff2ed2d1a1025eb8c6952c18f63d93dc8..b830de22c3db6d81ba9f45cb642abe3aeb0c6bcd 100644 --- a/include/simulator.h +++ b/include/simulator.h @@ -23,7 +23,7 @@ public: // Constructor ThreadUpdateInfo(std::vector agents, unsigned int bMin, unsigned int bMax) : - bMin_(bMin), bMax_(bMax) + bMin_(bMin), bMax_(bMax) { ag_.insert(ag_.end(), agents.begin() + bMin, agents.begin() + bMax) ; } @@ -36,7 +36,8 @@ public: void run() { // Thread execution stuff goes here - for (unsigned int i = 0 ; i < ag_.size() ; ++i) { + for (unsigned int i = 0; i < ag_.size(); ++i) + { // ag_[i]->updateObstacleNeighbors(); // ag_[i]->updateAgentNeighbors(); ag_[i]->computePrefVelocity() ; @@ -54,7 +55,7 @@ public: // Constructor ThreadUpdatePos(std::vector agents, unsigned int bMin, unsigned int bMax) : - bMin_(bMin), bMax_(bMax) + bMin_(bMin), bMax_(bMax) { ag_.insert(ag_.end(), agents.begin() + bMin, agents.begin() + bMax) ; } @@ -67,7 +68,8 @@ public: void run() { // Thread execution stuff goes here - for (unsigned int i = 0 ; i < ag_.size() ; ++i) { + for (unsigned int i = 0; i < ag_.size(); ++i) + { ag_[i]->update() ; } } @@ -76,15 +78,14 @@ public: class Simulator { public: - Simulator() ; + Simulator(int minSize) ; Simulator(float timeStep, float neighborDist, unsigned int maxNeighbors, float timeHorizon, - float timeHorizonObst, float radius, float maxSpeed, - const VEC3& velocity = VEC3(0)) ; + float timeHorizonObst, float radius, float maxSpeed, const VEC3& velocity = VEC3(0)) ; ~Simulator() ; - void init(unsigned int config, float dimension, bool enablePathFinding = false) ; + void init(unsigned int config, int minSize, float dimension, bool enablePathFinding = false) ; void setupCircleScenario(unsigned int nbMaxAgent) ; void setupCorridorScenario(unsigned int nbMaxAgent) ; void setupCityScenario(float startX, float startY, int nbLines, int nbRank) ; @@ -94,7 +95,6 @@ public: bool reachedGoal() ; - void setupMovingObstacle(unsigned int nbObstacles) ; #ifndef SPATIAL_HASHING diff --git a/include/spatialHashing.h b/include/spatialHashing.h index 91952c2e0505d5a18e74b1bff09e33bb5e9cf902..407d90d4d62cd85de6980e959bf39902d6d21aa3 100644 --- a/include/spatialHashing.h +++ b/include/spatialHashing.h @@ -58,9 +58,10 @@ template class HashTable2D { public: - typedef std::list< std::pair > cell_type ; + typedef std::list > cell_type ; - HashTable2D(unsigned int s) : m_size(s) + HashTable2D(unsigned int s) : + m_size(s) { m_table = new cell_type[s] ; } @@ -70,42 +71,46 @@ public: delete[] m_table ; } - unsigned int size() { return m_size ; } + unsigned int size() + { + return m_size ; + } - cell_type& cell(unsigned int i) { return m_table[i] ; } + cell_type& cell(unsigned int i) + { + return m_table[i] ; + } - DATA& operator[] (const Geom::Vec2ui& k) + DATA& operator[](const Geom::Vec2ui& k) { unsigned int c = hash(k) ; - for(typename cell_type::iterator it = m_table[c].begin(); it != m_table[c].end(); ++it) + for (typename cell_type::iterator it = m_table[c].begin(); it != m_table[c].end(); ++it) { - if(it->first == k) - return it->second ; + if (it->first == k) return it->second ; } m_table[c].push_back(std::make_pair(k, DATA())) ; return m_table[c].back().second ; } - const DATA& operator[] (const Geom::Vec2ui& k) const - { - unsigned int c = hash(k) ; - - for(typename cell_type::iterator it = m_table[c].begin(); it != m_table[c].end(); ++it) - { - if(it->first == k) - return it->second ; - } - } + // TODO : pas de valeur de retour si la valeur n'est pas trouvée +// const DATA& operator[](const Geom::Vec2ui& k) const +// { +// unsigned int c = hash(k) ; +// +// for (typename cell_type::iterator it = m_table[c].begin(); it != m_table[c].end(); ++it) +// { +// if (it->first == k) return it->second ; +// } +// } bool hasData(const Geom::Vec2ui& k) { unsigned int c = hash(k) ; - for(typename cell_type::iterator it = m_table[c].begin(); it != m_table[c].end(); ++it) + for (typename cell_type::iterator it = m_table[c].begin(); it != m_table[c].end(); ++it) { - if(it->first == k) - return true ; + if (it->first == k) return true ; } return false ; } @@ -114,9 +119,9 @@ public: { unsigned int c = hash(k) ; - for(typename cell_type::iterator it = m_table[c].begin(); it != m_table[c].end(); ++it) + for (typename cell_type::iterator it = m_table[c].begin(); it != m_table[c].end(); ++it) { - if(it->first == k) + if (it->first == k) { m_table[c].erase(it) ; return ; @@ -127,7 +132,7 @@ public: private: unsigned int hash(const Geom::Vec2ui& k) { - return ( (k[0] * 73856093) ^ (k[1] * 19349663) ) % m_size ; + return ((k[0] * 73856093) ^ (k[1] * 19349663)) % m_size ; } unsigned int m_size ; diff --git a/include/utils.h b/include/utils.h index 9c872337a2190e10f60668119aac69be0eaa6359..8d9c90a60f03fb0dd59bf4c3a0004162292e7058 100644 --- a/include/utils.h +++ b/include/utils.h @@ -7,49 +7,49 @@ #include "Geometry/vector_gen.h" -typedef CGoGN::Geom::Vec3f VEC3; +typedef CGoGN::Geom::Vec3f VEC3 ; -static const float RVO_EPSILON = 0.00001f; +static const float RVO_EPSILON = 0.00001f ; struct Line { - VEC3 point; - VEC3 direction; -}; + VEC3 point ; + VEC3 direction ; +} ; inline float det2D(const VEC3& vector1, const VEC3& vector2) { - return vector1[0] * vector2[1] - vector1[1] * vector2[0]; + return vector1[0] * vector2[1] - vector1[1] * vector2[0] ; } inline float distSqPointLineSegment(VEC3 a1, VEC3 b1, const VEC3& c1) { - a1[2] = 0; - b1[2] = 0; + a1[2] = 0 ; + b1[2] = 0 ; - VEC3 a1b1 = b1 - a1; - VEC3 a1c1 = c1 - a1; - const float r = (a1c1 * a1b1) / a1b1.norm2(); + VEC3 a1b1 = b1 - a1 ; + VEC3 a1c1 = c1 - a1 ; + const float r = (a1c1 * a1b1) / a1b1.norm2() ; if (r < 0.0f) { - return a1c1.norm2(); + return a1c1.norm2() ; } else if (r > 1.0f) { - VEC3 b1c1 = c1 - b1; - return b1c1.norm2(); + VEC3 b1c1 = c1 - b1 ; + return b1c1.norm2() ; } else { - VEC3 v = c1 - (a1 + r * a1b1); - return v.norm2(); + VEC3 v = c1 - (a1 + r * a1b1) ; + return v.norm2() ; } } inline float leftOf(const VEC3& a, const VEC3& b, const VEC3& c) { - return det2D(a - c, b - a); + return det2D(a - c, b - a) ; } #endif diff --git a/include/viewer.h b/include/viewer.h index 79584010291d5c250940c2cd43824ce21b203cca..87b3fcef1113e9f015f93cd321b0d82a0c5cac03 100644 --- a/include/viewer.h +++ b/include/viewer.h @@ -1,28 +1,29 @@ /******************************************************************************* -* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps * -* version 0.1 * -* Copyright (C) 2009, 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: https://iggservis.u-strasbg.fr/CGoGN/ * -* Contact information: cgogn@unistra.fr * -* * -*******************************************************************************/ + * CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps * + * version 0.1 * + * Copyright (C) 2009, 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: https://iggservis.u-strasbg.fr/CGoGN/ * + * Contact information: cgogn@unistra.fr * + * * + *******************************************************************************/ #include +#include #include "Utils/Qt/qtSimple.h" #include "ui_socialAgents.h" @@ -43,14 +44,14 @@ using namespace CGoGN ; -typedef PFP::MAP MAP; +typedef PFP::MAP MAP ; class SocialAgents : public Utils::QT::SimpleQT { Q_OBJECT public: - SocialAgents() ; + SocialAgents(int minSize = Agent::neighborDist_, int iterations = 0) ; void initGUI() ; @@ -58,28 +59,31 @@ public: void cb_redraw() ; #ifndef SPATIAL_HASHING - void exportInfoFace(std::ofstream& out, Dart d); - bool exportScenePov(PFP::MAP& map, PFP::TVEC3& position, const std::string& filename, PFP::VEC3 cameraPos, PFP::VEC3 cameraLook, PFP::VEC3 translate, float angle_X, float angle_Y, float angle_Z,const FunctorSelect& good = SelectorTrue()); + void exportInfoFace(std::ofstream& out, Dart d) ; + bool exportScenePov(PFP::MAP& map, PFP::TVEC3& position, const std::string& filename, + PFP::VEC3 cameraPos, PFP::VEC3 cameraLook, PFP::VEC3 translate, + float angle_X, float angle_Y, float angle_Z, + const FunctorSelect& good = SelectorTrue()) ; #endif - + void cb_keyPress(int keycode) ; - Utils::QT::uiDockInterface dock ; + Utils::QT::uiDockInterface dock ; - QTimer* timer; + QTimer* timer ; + int nbIterations ; + // Number of iterations before stopping the animation (0 if interactive animation) + int maxIterations ; // to count fps - int frames; - float nextUpdate; - bool displayFps; - float fps; - - Simulator sim; - SelectorTrue allDarts; - - bool render_anim; - int visu; - int nbGenerated; + int frames ; + clock_t nextUpdate ; + + Simulator sim ; + SelectorTrue allDarts ; + + bool render_anim ; + int visu ; bool drawEnvLines ; bool drawEnvFaces ; @@ -93,13 +97,48 @@ public: public slots: void animate() ; - void slot_timer(bool b) { b ? timer->start() : timer->stop() ; } - void slot_drawEnvLines(bool b) { drawEnvLines = b ; updateGL() ; } - void slot_drawEnvFaces(bool b) { drawEnvFaces = b ; updateGL() ; } - void slot_drawEnvTopo(bool b) { drawEnvTopo = b ; updateGL() ; } - void slot_drawObstacles(bool b) { drawObstacles = b ; updateGL() ; } - void slot_drawAgents(bool b) { drawAgents = b ; updateGL() ; } - void slot_drawAgentsPredictionTri(bool b) { drawAgentsPredictionTri = b ; updateGL() ; } - void slot_drawAgentsNeighborDist(bool b) { drawAgentsNeighborDist = b ; updateGL() ; } - void slot_drawAgentsObstacleDist(bool b) { drawAgentsObstacleDist = b ; updateGL() ; } + void slot_timer(bool b) + { + b ? timer->start() : timer->stop() ; + } + void slot_drawEnvLines(bool b) + { + drawEnvLines = b ; + updateGL() ; + } + void slot_drawEnvFaces(bool b) + { + drawEnvFaces = b ; + updateGL() ; + } + void slot_drawEnvTopo(bool b) + { + drawEnvTopo = b ; + updateGL() ; + } + void slot_drawObstacles(bool b) + { + drawObstacles = b ; + updateGL() ; + } + void slot_drawAgents(bool b) + { + drawAgents = b ; + updateGL() ; + } + void slot_drawAgentsPredictionTri(bool b) + { + drawAgentsPredictionTri = b ; + updateGL() ; + } + void slot_drawAgentsNeighborDist(bool b) + { + drawAgentsNeighborDist = b ; + updateGL() ; + } + void slot_drawAgentsObstacleDist(bool b) + { + drawAgentsObstacleDist = b ; + updateGL() ; + } } ; diff --git a/src/agent.cpp b/src/agent.cpp index 4a23aeb185896944111c36a2ff97d3fe2de8a16b..492f57db5c92eae10dd1a7ce178c239bb176dd9e 100644 --- a/src/agent.cpp +++ b/src/agent.cpp @@ -3,7 +3,7 @@ unsigned int Agent::maxNeighbors_ = 10 ; float Agent::maxSpeed_ = 2.0f ; -float Agent::neighborDist_ = 15.0f ; +float Agent::neighborDist_ = 20.0f ; float Agent::neighborDistSq_ = neighborDist_ * neighborDist_ ; float Agent::radius_ = 1.5f ; float Agent::timeHorizon_ = 10.0f ; @@ -32,12 +32,16 @@ sim_(sim) } #else Agent::Agent(Simulator* sim, const VEC3& position, Dart d) : - part_(sim->envMap_.map, d, position, sim->envMap_.position), curGoal_(-1), velocity_(0), newVelocity_( - 0), prefVelocity_(0), meanSpeed_(0), sim_(sim) + part_(sim->envMap_.map, d, position, sim->envMap_.position), + curGoal_(-1), + velocity_(0), + newVelocity_(0), + prefVelocity_(0), + meanSpeed_(0), + sim_(sim) { agentNeighbors_.reserve(maxNeighbors_ * 2) ; obstacleNeighbors_.reserve(maxNeighbors_ * 2) ; - timeStep = sim->timeStep_ ; agentNo = cptAgent++ ; } #endif @@ -75,22 +79,27 @@ void Agent::updateAgentNeighbors() #endif float maxDist = 0.0f ; - for (std::vector::const_iterator it = agents.begin() ; it != agents.end() ; ++it) { - if (*it != this) { + for (std::vector::const_iterator it = agents.begin(); it != agents.end(); ++it) + { + if (*it != this) + { float distSq = (getPosition() - (*it)->getPosition()).norm2() ; if ((agentNeighbors_.size() < maxNeighbors_ || distSq < maxDist) - && distSq < neighborDistSq_) { + && distSq < neighborDistSq_) + { if (distSq > maxDist) maxDist = distSq ; agentNeighbors_.push_back(std::make_pair(distSq, *it)) ; } } } - for (std::vector::const_iterator it = neighborAgents.begin() ; - it != neighborAgents.end() ; ++it) { + for (std::vector::const_iterator it = neighborAgents.begin(); + it != neighborAgents.end(); ++it) + { float distSq = (getPosition() - (*it)->getPosition()).norm2() ; if ((agentNeighbors_.size() < maxNeighbors_ || distSq < maxDist) - && distSq < neighborDistSq_) { + && distSq < neighborDistSq_) + { if (distSq > maxDist) maxDist = distSq ; agentNeighbors_.push_back(std::make_pair(distSq, *it)) ; } @@ -100,7 +109,8 @@ void Agent::updateAgentNeighbors() sim_->nearNeighbors += agentNeighbors_.size() ; sim_->totalNeighbors += agents.size() + neighborAgents.size() ; - if (agentNeighbors_.size() > maxNeighbors_) { + if (agentNeighbors_.size() > maxNeighbors_) + { sim_->nbSorts++ ; std::sort(agentNeighbors_.begin(), agentNeighbors_.end(), agentSort) ; } @@ -127,17 +137,20 @@ void Agent::updateObstacleNeighbors() } #else std::vector& obst = sim_->envMap_.obstvect[part_.d] ; - for (std::vector::const_iterator it = obst.begin() ; it != obst.end() ; ++it) { + for (std::vector::const_iterator it = obst.begin(); it != obst.end(); ++it) + { float distSq = distSqPointLineSegment((*it)->p1, (*it)->p2, part_.m_position) ; - if (distSq < rangeSq_) { - if (Geom::testOrientation2D(part_.m_position, (*it)->p1, (*it)->p2) == Geom::RIGHT) - obstacleNeighbors_.push_back(std::make_pair(distSq, *it)) ; + if (distSq < rangeSq_) + { + if (Geom::testOrientation2D(part_.m_position, (*it)->p1, (*it)->p2) == Geom::RIGHT) obstacleNeighbors_.push_back( + std::make_pair(distSq, *it)) ; } } #endif - if (obstacleNeighbors_.size() > maxNeighbors_) - std::sort(obstacleNeighbors_.begin(), obstacleNeighbors_.end(), obstacleSort) ; + if (obstacleNeighbors_.size() > maxNeighbors_) std::sort(obstacleNeighbors_.begin(), + obstacleNeighbors_.end(), + obstacleSort) ; } void Agent::update() @@ -168,7 +181,8 @@ void Agent::computePrefVelocity() VEC3 goalVector = goals_[curGoal_] - getPosition() ; float goalDist2 = goalVector.norm2() ; - if (goalDist2 < 35.0f) { + if (goalDist2 < 35.0f) + { curGoal_ = (curGoal_ + 1) % goals_.size() ; goalVector = goals_[curGoal_] - getPosition() ; goalDist2 = goalVector.norm2() ; @@ -191,19 +205,22 @@ void Agent::computeNewVelocity2() c.zero() ; vel.zero() ; - for (std::vector >::iterator it = obstacleNeighbors_.begin() ; - it != obstacleNeighbors_.end() ; ++it) { + for (std::vector >::iterator it = obstacleNeighbors_.begin(); + it != obstacleNeighbors_.end(); ++it) + { } unsigned int nbA = 0 ; - for (std::vector >::iterator it = agentNeighbors_.begin() ; - it != agentNeighbors_.end() && nbA < maxNeighbors_ ; ++it , ++nbA) { + for (std::vector >::iterator it = agentNeighbors_.begin(); + it != agentNeighbors_.end() && nbA < maxNeighbors_; ++it, ++nbA) + { VEC3 mPos = getPosition() ; VEC3 oPos = (*it).second->getPosition() ; centroid += oPos ; VEC3 pOp(oPos - mPos) ; - if (pOp.norm2() <= radius_ * radius_ * maxSpeed_ * maxSpeed_ * 2.0f) { + if (pOp.norm2() <= radius_ * radius_ * maxSpeed_ * maxSpeed_ * 2.0f) + { c -= pOp ; // prefVelocity_.zero(); } @@ -211,7 +228,8 @@ void Agent::computeNewVelocity2() vel += (*it).second->prefVelocity_ ; } - if (nbA > 0) { + if (nbA > 0) + { centroid /= nbA ; centroid -= getPosition() ; centroid /= 10 ; @@ -233,8 +251,9 @@ void Agent::computeNewVelocity() float invTimeHorizonObst = 1.0f / timeHorizonObst_ ; /* Create obstacle ORCA lines. */ - for (std::vector >::iterator it = obstacleNeighbors_.begin() ; - it != obstacleNeighbors_.end() ; ++it) { + for (std::vector >::iterator it = obstacleNeighbors_.begin(); + it != obstacleNeighbors_.end(); ++it) + { const Obstacle* obst = it->second ; const VEC3 relativePosition1(obst->p1 - getPosition()) ; @@ -246,12 +265,13 @@ void Agent::computeNewVelocity() */ bool alreadyCovered = false ; - for (unsigned int j = 0 ; j < orcaLines_.size() ; ++j) { + for (unsigned int j = 0; j < orcaLines_.size(); ++j) + { if (det2D(invTimeHorizonObst * relativePosition1 - orcaLines_[j].point, - orcaLines_[j].direction) - invTimeHorizonObst * radius_ >= -RVO_EPSILON - && det2D(invTimeHorizonObst * relativePosition2 - orcaLines_[j].point, - orcaLines_[j].direction) - invTimeHorizonObst * radius_ - >= -RVO_EPSILON) { + orcaLines_[j].direction) - invTimeHorizonObst * radius_ >= -RVO_EPSILON + && det2D(invTimeHorizonObst * relativePosition2 - orcaLines_[j].point, + orcaLines_[j].direction) - invTimeHorizonObst * radius_ >= -RVO_EPSILON) + { alreadyCovered = true ; break ; } @@ -275,7 +295,8 @@ void Agent::computeNewVelocity() Line line ; - if (s < 0 && distSq1 <= radiusSq) { + if (s < 0 && distSq1 <= radiusSq) + { /* Collision with left vertex. Ignore if non-convex. */ // if (obstacle1->isConvex_) // { @@ -286,7 +307,8 @@ void Agent::computeNewVelocity() // } continue ; } - else if (s > 1 && distSq2 <= radiusSq) { + else if (s > 1 && distSq2 <= radiusSq) + { /* Collision with right vertex. Ignore if non-convex * or if it will be taken care of by neighboring obstacle */ // VEC3 nextObstacleVectorN(nextObstacleVector); @@ -300,7 +322,8 @@ void Agent::computeNewVelocity() // } continue ; } - else if (s >= 0 && s < 1 && distSqLine <= radiusSq) { + else if (s >= 0 && s < 1 && distSqLine <= radiusSq) + { /* Collision with obstacle segment. */ line.point = VEC3(0) ; line.direction = VEC3(-1.0f * obstacleVector) ; @@ -315,11 +338,12 @@ void Agent::computeNewVelocity() * vertex. Legs extend cut-off line when nonconvex vertex. */ - VEC3 leftLegDirection , rightLegDirection ; + VEC3 leftLegDirection, rightLegDirection ; bool obst1EQobst2 = false ; bool obst2EQobst1 = false ; - if (s < 0 && distSqLine <= radiusSq) { + if (s < 0 && distSqLine <= radiusSq) + { /* * Obstacle viewed obliquely so that left vertex * defines velocity obstacle. @@ -333,15 +357,15 @@ void Agent::computeNewVelocity() obst2EQobst1 = true ; const float leg1 = std::sqrt(distSq1 - radiusSq) ; - leftLegDirection = VEC3( - relativePosition1[0] * leg1 - relativePosition1[1] * radius_, - relativePosition1[0] * radius_ + relativePosition1[1] * leg1, 0) / distSq1 ; - rightLegDirection = VEC3( - relativePosition1[0] * leg1 + relativePosition1[1] * radius_, - -relativePosition1[0] * radius_ + relativePosition1[1] * leg1, 0) - / distSq1 ; + leftLegDirection = VEC3(relativePosition1[0] * leg1 - relativePosition1[1] * radius_, + relativePosition1[0] * radius_ + relativePosition1[1] * leg1, 0) + / distSq1 ; + rightLegDirection = VEC3(relativePosition1[0] * leg1 + relativePosition1[1] * radius_, + -relativePosition1[0] * radius_ + relativePosition1[1] * leg1, + 0) / distSq1 ; } - else if (s > 1 && distSqLine <= radiusSq) { + else if (s > 1 && distSqLine <= radiusSq) + { /* * Obstacle viewed obliquely so that right vertex * defines velocity obstacle. @@ -355,22 +379,22 @@ void Agent::computeNewVelocity() obst1EQobst2 = true ; const float leg2 = std::sqrt(distSq2 - radiusSq) ; - leftLegDirection = VEC3( - relativePosition2[0] * leg2 - relativePosition2[1] * radius_, - relativePosition2[0] * radius_ + relativePosition2[1] * leg2, 0) / distSq2 ; - rightLegDirection = VEC3( - relativePosition2[0] * leg2 + relativePosition2[1] * radius_, - -relativePosition2[0] * radius_ + relativePosition2[1] * leg2, 0) - / distSq2 ; + leftLegDirection = VEC3(relativePosition2[0] * leg2 - relativePosition2[1] * radius_, + relativePosition2[0] * radius_ + relativePosition2[1] * leg2, 0) + / distSq2 ; + rightLegDirection = VEC3(relativePosition2[0] * leg2 + relativePosition2[1] * radius_, + -relativePosition2[0] * radius_ + relativePosition2[1] * leg2, + 0) / distSq2 ; } - else { + else + { /* Usual situation. */ // if (obstacle1->isConvex_) // { const float leg1 = std::sqrt(distSq1 - radiusSq) ; - leftLegDirection = VEC3( - relativePosition1[0] * leg1 - relativePosition1[1] * radius_, - relativePosition1[0] * radius_ + relativePosition1[1] * leg1, 0) / distSq1 ; + leftLegDirection = VEC3(relativePosition1[0] * leg1 - relativePosition1[1] * radius_, + relativePosition1[0] * radius_ + relativePosition1[1] * leg1, 0) + / distSq1 ; // } // else // { @@ -381,10 +405,9 @@ void Agent::computeNewVelocity() // if (obstacle2->isConvex_) // { const float leg2 = std::sqrt(distSq2 - radiusSq) ; - rightLegDirection = VEC3( - relativePosition2[0] * leg2 + relativePosition2[1] * radius_, - -relativePosition2[0] * radius_ + relativePosition2[1] * leg2, 0) - / distSq2 ; + rightLegDirection = VEC3(relativePosition2[0] * leg2 + relativePosition2[1] * radius_, + -relativePosition2[0] * radius_ + relativePosition2[1] * leg2, + 0) / distSq2 ; // } // else // { @@ -405,7 +428,8 @@ void Agent::computeNewVelocity() VEC3 leftObstacle = obst1EQobst2 ? obstacleVector : previousObstacleVector ; leftObstacle.normalize() ; - if (/*obstacle1->isConvex_ && */det2D(leftLegDirection, -1.0f * leftObstacle) > 0.0f) { + if (/*obstacle1->isConvex_ && */det2D(leftLegDirection, -1.0f * leftObstacle) > 0.0f) + { /* Left leg points into obstacle. */ leftLegDirection = -1.0f * leftObstacle ; isLeftLegForeign = true ; @@ -414,7 +438,8 @@ void Agent::computeNewVelocity() VEC3 rightObstacle = obst2EQobst1 ? obstacleVector : nextObstacleVector ; rightObstacle.normalize() ; - if (/*obstacle2->isConvex_ && */det2D(rightLegDirection, rightObstacle) < 0.0f) { + if (/*obstacle2->isConvex_ && */det2D(rightLegDirection, rightObstacle) < 0.0f) + { /* Right leg points into obstacle. */ rightLegDirection = rightObstacle ; isRightLegForeign = true ; @@ -422,9 +447,9 @@ void Agent::computeNewVelocity() /* Compute cut-off centers. */ const VEC3 leftCutoff = invTimeHorizonObst - * ((obst1EQobst2 ? obst->p2 : obst->p1) - getPosition()) ; + * ((obst1EQobst2 ? obst->p2 : obst->p1) - getPosition()) ; const VEC3 rightCutoff = invTimeHorizonObst - * ((obst2EQobst1 ? obst->p1 : obst->p2) - getPosition()) ; + * ((obst2EQobst1 ? obst->p1 : obst->p2) - getPosition()) ; const VEC3 cutoffVec = rightCutoff - leftCutoff ; /* Project current velocity on velocity obstacle. */ @@ -436,7 +461,8 @@ void Agent::computeNewVelocity() const float tRight = ((velocity_ - rightCutoff) * rightLegDirection) ; if ((t < 0.0f && tLeft < 0.0f) - || (/*obst1 == obst2*/obst1EQobst2 && tLeft < 0.0f && tRight < 0.0f)) { + || (/*obst1 == obst2*/obst1EQobst2 && tLeft < 0.0f && tRight < 0.0f)) + { /* Project on left cut-off circle. */ VEC3 unitW = velocity_ - leftCutoff ; unitW.normalize() ; @@ -446,7 +472,8 @@ void Agent::computeNewVelocity() orcaLines_.push_back(line) ; continue ; } - else if (t > 1.0f && tRight < 0.0f) { + else if (t > 1.0f && tRight < 0.0f) + { /* Project on right cut-off circle. */ VEC3 unitW = velocity_ - rightCutoff ; unitW.normalize() ; @@ -465,43 +492,42 @@ void Agent::computeNewVelocity() VEC3 v2 = velocity_ - (leftCutoff + tLeft * leftLegDirection) ; VEC3 v3 = velocity_ - (rightCutoff + tRight * rightLegDirection) ; const float distSqCutoff = - (t < 0.0f || t > 1.0f || /*obst1 == obst2*/obst1EQobst2) ? - std::numeric_limits::infinity() : v1.norm2() ; + (t < 0.0f || t > 1.0f || /*obst1 == obst2*/obst1EQobst2) ? std::numeric_limits::infinity() : v1.norm2() ; const float distSqLeft = - (tLeft < 0.0f) ? std::numeric_limits::infinity() : v2.norm2() ; + (tLeft < 0.0f) ? std::numeric_limits::infinity() : v2.norm2() ; const float distSqRight = - (tRight < 0.0f) ? std::numeric_limits::infinity() : v3.norm2() ; + (tRight < 0.0f) ? std::numeric_limits::infinity() : v3.norm2() ; - if (distSqCutoff <= distSqLeft && distSqCutoff <= distSqRight) { + if (distSqCutoff <= distSqLeft && distSqCutoff <= distSqRight) + { /* Project on cut-off line. */ VEC3 dir = obst1EQobst2 ? nextObstacleVector : obstacleVector ; dir.normalize() ; line.direction = -1.0f * dir ; line.direction.normalize() ; line.point = leftCutoff - + radius_ * invTimeHorizonObst - * VEC3(-line.direction[1], line.direction[0], 0) ; + + radius_ * invTimeHorizonObst * VEC3(-line.direction[1], line.direction[0], 0) ; orcaLines_.push_back(line) ; continue ; } - else if (distSqLeft <= distSqRight) { + else if (distSqLeft <= distSqRight) + { /* Project on left leg. */ if (isLeftLegForeign) continue ; line.direction = leftLegDirection ; line.point = leftCutoff - + radius_ * invTimeHorizonObst - * VEC3(-line.direction[1], line.direction[0], 0) ; + + radius_ * invTimeHorizonObst * VEC3(-line.direction[1], line.direction[0], 0) ; orcaLines_.push_back(line) ; continue ; } - else { + else + { /* Project on right leg. */ if (isRightLegForeign) continue ; line.direction = -1.0f * rightLegDirection ; line.point = rightCutoff - + radius_ * invTimeHorizonObst - * VEC3(-line.direction[1], line.direction[0], 0) ; + + radius_ * invTimeHorizonObst * VEC3(-line.direction[1], line.direction[0], 0) ; orcaLines_.push_back(line) ; continue ; } @@ -513,8 +539,9 @@ void Agent::computeNewVelocity() /* Create agent ORCA lines. */ unsigned int nbA = 0 ; - for (std::vector >::iterator it = agentNeighbors_.begin() ; - it != agentNeighbors_.end() && nbA < maxNeighbors_ ; ++it , ++nbA) { + for (std::vector >::iterator it = agentNeighbors_.begin(); + it != agentNeighbors_.end() && nbA < maxNeighbors_; ++it, ++nbA) + { Agent* other = it->second ; const VEC3 relativePosition(other->getPosition() - getPosition()) ; @@ -527,15 +554,16 @@ void Agent::computeNewVelocity() VEC3 u ; /* No collision. */ - if (distSq > combinedRadiusSq) { + if (distSq > combinedRadiusSq) + { const VEC3 w(relativeVelocity - invTimeHorizon * relativePosition) ; const float wLengthSq = w.norm2() ; const float dotProduct1 = w * relativePosition ; /* Project on cut-off circle */ - if (dotProduct1 < 0.0f - && dotProduct1 * dotProduct1 > combinedRadiusSq * wLengthSq) { + if (dotProduct1 < 0.0f && dotProduct1 * dotProduct1 > combinedRadiusSq * wLengthSq) + { const float wLength = std::sqrt(wLengthSq) ; const VEC3 unitW = w / wLength ; @@ -543,22 +571,21 @@ void Agent::computeNewVelocity() u = (combinedRadius * invTimeHorizon - wLength) * unitW ; } /* Project on legs */ - else { + else + { const float leg = std::sqrt(distSq - combinedRadiusSq) ; if (det2D(relativePosition, w) > 0.0f) // Project on left leg - line.direction = VEC3( - relativePosition[0] * leg - relativePosition[1] * combinedRadius, - relativePosition[0] * combinedRadius + relativePosition[1] * leg, 0) - / distSq ; + line.direction = VEC3( + relativePosition[0] * leg - relativePosition[1] * combinedRadius, + relativePosition[0] * combinedRadius + relativePosition[1] * leg, 0) + / distSq ; else - // Project on right leg - line.direction = -1.0f - * VEC3( - relativePosition[0] * leg - + relativePosition[1] * combinedRadius, - -relativePosition[0] * combinedRadius - + relativePosition[1] * leg, 0) / distSq ; + // Project on right leg + line.direction = -1.0f + * VEC3(relativePosition[0] * leg + relativePosition[1] * combinedRadius, + -relativePosition[0] * combinedRadius + relativePosition[1] * leg, 0) + / distSq ; const float dotProduct2 = relativeVelocity * line.direction ; @@ -566,7 +593,8 @@ void Agent::computeNewVelocity() } } /* Collision. */ - else { + else + { /* Project on cut-off circle of time timeStep. */ const float invTimeStep = 1.0f / timeStep ; @@ -585,18 +613,18 @@ void Agent::computeNewVelocity() } unsigned int lineFail = linearProgram2(orcaLines_, maxSpeed_, prefVelocity_, false, - newVelocity_) ; + newVelocity_) ; - if (lineFail < orcaLines_.size()) - linearProgram3(orcaLines_, numObstLines, lineFail, maxSpeed_, newVelocity_) ; + if (lineFail < orcaLines_.size()) linearProgram3(orcaLines_, numObstLines, lineFail, maxSpeed_, + newVelocity_) ; } bool Agent::linearProgram1(const std::vector& lines, unsigned int lineNo, float radius, - const VEC3& optVelocity, bool directionOpt, VEC3& result) + const VEC3& optVelocity, bool directionOpt, VEC3& result) { const float dotProduct = lines[lineNo].point * lines[lineNo].direction ; const float discriminant = dotProduct * dotProduct + radius * radius - - lines[lineNo].point.norm2() ; + - lines[lineNo].point.norm2() ; /* Max speed circle fully invalidates line lineNo. */ if (discriminant < 0.0f) return false ; @@ -605,70 +633,83 @@ bool Agent::linearProgram1(const std::vector& lines, unsigned int lineNo, float tLeft = -dotProduct - sqrtDiscriminant ; float tRight = -dotProduct + sqrtDiscriminant ; - for (unsigned int i = 0 ; i < lineNo ; ++i) { + for (unsigned int i = 0; i < lineNo; ++i) + { const float denominator = det2D(lines[lineNo].direction, lines[i].direction) ; - const float numerator = det2D(lines[i].direction, - lines[lineNo].point - lines[i].point) ; + const float numerator = det2D(lines[i].direction, lines[lineNo].point - lines[i].point) ; if (std::fabs(denominator) <= RVO_EPSILON) // Lines lineNo and i are (almost) parallel. - { - if (numerator < 0.0f) return false ; - else continue ; + { + if (numerator < 0.0f) + return false ; + else + continue ; } const float t = numerator / denominator ; if (denominator >= 0.0f) // Line i bounds line lineNo on the right - tRight = std::min(tRight, t) ; + tRight = std::min(tRight, t) ; else - // Line i bounds line lineNo on the left - tLeft = std::max(tLeft, t) ; + // Line i bounds line lineNo on the left + tLeft = std::max(tLeft, t) ; if (tLeft > tRight) return false ; } /* Optimize direction. */ - if (directionOpt) { + if (directionOpt) + { if (optVelocity * lines[lineNo].direction > 0.0f) // Take right extreme - result = lines[lineNo].point + tRight * lines[lineNo].direction ; + result = lines[lineNo].point + tRight * lines[lineNo].direction ; else - // Take left extreme - result = lines[lineNo].point + tLeft * lines[lineNo].direction ; + // Take left extreme + result = lines[lineNo].point + tLeft * lines[lineNo].direction ; } /* Optimize closest point. */ - else { + else + { const float t = lines[lineNo].direction * (optVelocity - lines[lineNo].point) ; - if (t < tLeft) result = lines[lineNo].point + tLeft * lines[lineNo].direction ; - else if (t > tRight) result = lines[lineNo].point + tRight * lines[lineNo].direction ; - else result = lines[lineNo].point + t * lines[lineNo].direction ; + if (t < tLeft) + result = lines[lineNo].point + tLeft * lines[lineNo].direction ; + else if (t > tRight) + result = lines[lineNo].point + tRight * lines[lineNo].direction ; + else + result = lines[lineNo].point + t * lines[lineNo].direction ; } return true ; } unsigned int Agent::linearProgram2(const std::vector& lines, float radius, - const VEC3& optVelocity, bool directionOpt, VEC3& result) + const VEC3& optVelocity, bool directionOpt, VEC3& result) { /* * Optimize direction. Note that the optimization velocity is of unit * length in this case. */ - if (directionOpt) result = optVelocity * radius ; + if (directionOpt) + result = optVelocity * radius ; /* Optimize closest point and outside circle. */ - else if (optVelocity.norm2() > radius * radius) { + else if (optVelocity.norm2() > radius * radius) + { VEC3 optVelocityN(optVelocity) ; optVelocityN.normalize() ; result = optVelocityN * radius ; } /* Optimize closest point and inside circle. */ - else result = optVelocity ; + else + result = optVelocity ; - for (unsigned int i = 0 ; i < lines.size() ; ++i) { - if (det2D(lines[i].direction, lines[i].point - result) > 0.0f) { + for (unsigned int i = 0; i < lines.size(); ++i) + { + if (det2D(lines[i].direction, lines[i].point - result) > 0.0f) + { /* Result does not satisfy constraint i. Compute new optimal result. */ const VEC3 tempResult(result) ; - if (!linearProgram1(lines, i, radius, optVelocity, directionOpt, result)) { + if (!linearProgram1(lines, i, radius, optVelocity, directionOpt, result)) + { result = tempResult ; return i ; } @@ -679,28 +720,34 @@ unsigned int Agent::linearProgram2(const std::vector& lines, float radius, } void Agent::linearProgram3(const std::vector& lines, unsigned int numObstLines, - unsigned int beginLine, float radius, VEC3& result) + unsigned int beginLine, float radius, VEC3& result) { float distance = 0.0f ; - for (unsigned int i = beginLine ; i < lines.size() ; ++i) { - if (det2D(lines[i].direction, lines[i].point - result) > distance) { + for (unsigned int i = beginLine; i < lines.size(); ++i) + { + if (det2D(lines[i].direction, lines[i].point - result) > distance) + { /* Result does not satisfy constraint of line i. */ std::vector projLines(lines.begin(), lines.begin() + numObstLines) ; - for (unsigned int j = numObstLines ; j < i ; ++j) { + for (unsigned int j = numObstLines; j < i; ++j) + { Line line ; float determinant = det2D(lines[i].direction, lines[j].direction) ; if (std::fabs(determinant) <= RVO_EPSILON) // Line i and line j are (almost) parallel. - { - if (lines[i].direction * lines[j].direction > 0.0f) continue ; // Line i and line j point in the same direction. - else line.point = 0.5f * (lines[i].point + lines[j].point) ; // Line i and line j point in opposite direction. + { + if (lines[i].direction * lines[j].direction > 0.0f) + continue ; // Line i and line j point in the same direction. + else + line.point = 0.5f * (lines[i].point + lines[j].point) ; // Line i and line j point in opposite direction. } - else line.point = lines[i].point - + (det2D(lines[j].direction, lines[i].point - lines[j].point) - / determinant) * lines[i].direction ; + else + line.point = lines[i].point + + (det2D(lines[j].direction, lines[i].point - lines[j].point) / determinant) + * lines[i].direction ; line.direction = lines[j].direction - lines[i].direction ; line.direction.normalize() ; @@ -715,8 +762,8 @@ void Agent::linearProgram3(const std::vector& lines, unsigned int numObstL * kept. */ if (linearProgram2(projLines, radius, - VEC3(-lines[i].direction[1], lines[i].direction[0], 0.0f), true, result) - < projLines.size()) result = tempResult ; + VEC3(-lines[i].direction[1], lines[i].direction[0], 0.0f), true, + result) < projLines.size()) result = tempResult ; distance = det2D(lines[i].direction, lines[i].point - result) ; } diff --git a/src/env_map.cpp b/src/env_map.cpp index b28f636b900acfbba3a840805ad5313356bda90f..a7645c376ba31dd53efc4f766eeca7e4fdc8d306 100644 --- a/src/env_map.cpp +++ b/src/env_map.cpp @@ -15,16 +15,19 @@ using namespace CGoGN ; EnvMap::EnvMap() : - obstacleDistance(Agent::range_), obstacleMarkS(mapScenary, EDGE), buildingMarkS( - mapScenary, FACE), obstacleMark(map, EDGE), buildingMark(map, FACE), pedWayMark( - map, FACE) + obstacleDistance(Agent::range_), + obstacleMarkS(mapScenary, EDGE), + buildingMarkS(mapScenary, FACE), + obstacleMark(map, EDGE), + buildingMark(map, FACE), + pedWayMark(map, FACE), #ifndef SPATIAL_HASHING - , refineMark(map, FACE), coarsenMark(map, FACE) + refineMark(map, FACE), + coarsenMark(map, FACE) #else - , - ht_agents(1024), - ht_neighbor_agents(1024), - ht_obstacles(1024) +ht_agents(1024), +ht_neighbor_agents(1024), +ht_obstacles(1024) #endif { position = map.addAttribute(VERTEX, "position") ; @@ -44,7 +47,7 @@ EnvMap::EnvMap() : void EnvMap::init(unsigned int config, REAL width, REAL height, REAL minSize, REAL maxSize) { std::cout << "Init EnvMap" << std::endl ; - VEC3 bottomLeft( -width / 2, -height / 2, 0.0f) ; + VEC3 bottomLeft(-width / 2, -height / 2, 0.0f) ; VEC3 topRight(width / 2, height / 2, 0.0f) ; geometry.reset() ; @@ -55,27 +58,25 @@ void EnvMap::init(unsigned int config, REAL width, REAL height, REAL minSize, RE std::cout << " - Geometry : " << geometry ; std::cout << " - Cell size between : " << minSize << " and " << maxSize << std::endl ; #ifdef SPATIAL_HASHING - a_cell_nb_x = geometry.size(0) / minCellSize ; - a_cell_nb_y = geometry.size(1) / minCellSize ; - o_cell_nb_x = geometry.size(0) / obstacleDistance ; - o_cell_nb_y = geometry.size(1) / obstacleDistance ; - std::cout << " - Table de hachage : " << a_cell_nb_x << " x " << a_cell_nb_y << std::endl ; + std::cout << " - Table de hachage : " << agentGridSize(0) << " x " << agentGridSize(1) << std::endl ; #endif unsigned int nbSquares = 6 ; - switch (config) { - case 0 : - CityGenerator::generateGrid( *this) ; - break ; - case 1 : - CityGenerator::generateGrid( *this) ; - break ; - case 2 : - CityGenerator::generateCity( *this) ; - // CityGenerator::generateMall(map, position, obstacleMark, buildingMark, sideSize); - break ; - case 3 : { + switch (config) + { + case 0 : + CityGenerator::generateGrid(*this) ; + break ; + case 1 : + CityGenerator::generateGrid(*this) ; + break ; + case 2 : + CityGenerator::generateCity(*this) ; + // CityGenerator::generateMall(map, position, obstacleMark, buildingMark, sideSize); + break ; + case 3 : + { // std::string filename = "/home/jund/Desktop/drawingQuads.svg" ; // std::string filename = "/home/jund/Desktop/drawingTest.svg" ; // std::string filename = "/home/jund/Desktop/drawingSimple.svg" ; @@ -89,12 +90,12 @@ void EnvMap::init(unsigned int config, REAL width, REAL height, REAL minSize, RE // map.closeMap() ; // Algo::Modelisation::CatmullClarkSubdivision(map, position) ; // Algo::Modelisation::computeDual(map) ; - } - break ; - case 4 : - CityGenerator::generatePlanet(map, position, obstacleMark, buildingMark, - 200.0f, nbSquares) ; - break ; + } + break ; + case 4 : + CityGenerator::generatePlanet(map, position, obstacleMark, buildingMark, 200.0f, + nbSquares) ; + break ; } // CityGenerator::simplifyFreeSpace(map, position, obstacleMark, buildingMark); @@ -102,13 +103,18 @@ void EnvMap::init(unsigned int config, REAL width, REAL height, REAL minSize, RE // CityGenerator::installGuardRail(map, position, obstacleMark, buildingMark, 5.0f); #ifndef SPATIAL_HASHING +// for (Dart d = map.begin() ; d != map.end(); map.next(d)) { +// Dart d1 = map.phi1(d); +// Dart d11= map.phi1(d1); +// if (map.phi1(d11) != d) map.splitFace(d,d11); +// } map.init() ; // registerObstaclesInFaces(); // subdivideAllToMaxLevel(); - for (unsigned int i = subdivisableFace.begin() ; i < subdivisableFace.end() ; - subdivisableFace.next(i)) + for (unsigned int i = subdivisableFace.begin(); i < subdivisableFace.end(); + subdivisableFace.next(i)) subdivisableFace[i].first = false ; #endif } @@ -116,15 +122,20 @@ void EnvMap::init(unsigned int config, REAL width, REAL height, REAL minSize, RE void EnvMap::markPedWay() { CellMarker treat(map, FACE) ; - for (Dart d = map.begin() ; d != map.end() ; map.next(d)) { - if ( !treat.isMarked(d)) { + for (Dart d = map.begin(); d != map.end(); map.next(d)) + { + if (!treat.isMarked(d)) + { treat.mark(d) ; Dart dd = d ; - do { + do + { Dart ddd = dd ; - do { - if (obstacleMark.isMarked(dd)) { + do + { + if (obstacleMark.isMarked(dd)) + { pedWayMark.mark(d) ; break ; } @@ -138,16 +149,19 @@ void EnvMap::markPedWay() } treat.unmarkAll() ; - for (Dart d = map.begin() ; d != map.end() ; map.next(d)) { - if ( !treat.isMarked(d)) { + for (Dart d = map.begin(); d != map.end(); map.next(d)) + { + if (!treat.isMarked(d)) + { treat.mark(d) ; Dart dd = d ; - do { - if (pedWayMark.isMarked(map.phi2(dd)) - && pedWayMark.isMarked(map.phi2(map.phi1(dd))) - && !pedWayMark.isMarked(map.phi2(map.phi1(map.phi1(dd)))) - && !pedWayMark.isMarked(map.phi2(map.phi1(map.phi1(map.phi1(dd)))))) { + do + { + if (pedWayMark.isMarked(map.phi2(dd)) && pedWayMark.isMarked(map.phi2(map.phi1(dd))) + && !pedWayMark.isMarked(map.phi2(map.phi1(map.phi1(dd)))) + && !pedWayMark.isMarked(map.phi2(map.phi1(map.phi1(map.phi1(dd)))))) + { pedWayMark.mark(d) ; break ; } @@ -162,46 +176,52 @@ void EnvMap::markPedWay() unsigned int EnvMap::mapMemoryCost() { return (map.getAttributeContainer(DART)).memorySize() - + (map.getAttributeContainer(VERTEX)).memorySize() - + (map.getAttributeContainer(EDGE)).memorySize() - + (map.getAttributeContainer(FACE)).memorySize() ; + + (map.getAttributeContainer(VERTEX)).memorySize() + + (map.getAttributeContainer(EDGE)).memorySize() + + (map.getAttributeContainer(FACE)).memorySize() ; } #ifndef SPATIAL_HASHING void EnvMap::subdivideAllToMaxLevel() { bool subdiv ; - do { + do + { subdiv = false ; { CellMarker subd(map, FACE) ; - for (Dart d = map.begin() ; d != map.end() ; map.next(d)) { - if ( !subd.isMarked(d)) { + for (Dart d = map.begin(); d != map.end(); map.next(d)) + { + if (!subd.isMarked(d)) + { subd.mark(d) ; - if ( !buildingMark.isMarked(d)) { + if (!buildingMark.isMarked(d)) + { //check if subdivision is authorized float minDistSq = Agent::neighborDistSq_ ; bool subdivisable = true ; Dart old = map.faceOldestDart(d) ; unsigned int fLevel = map.faceLevel(old) ; map.setCurrentLevel(fLevel) ; - PFP::VEC3 fCenter = Algo::Geometry::faceCentroid(map, old, - position) ; + PFP::VEC3 fCenter = Algo::Geometry::faceCentroid(map, old, position) ; Dart fd = old ; - do { + do + { PFP::VEC3& p = position[fd] ; PFP::VEC3 edge = Algo::Geometry::vectorOutOfDart(map, fd, - position) ; + position) ; PFP::VEC3 proj = fCenter - - (p + (edge * (fCenter - p) / edge.norm2()) * edge) ; - if (proj.norm2() < minDistSq) { + - (p + (edge * (fCenter - p) / edge.norm2()) * edge) ; + if (proj.norm2() < minDistSq) + { subdivisable = false ; break ; } fd = map.phi1(fd) ; } while (fd != old) ; - if (subdivisable) { + if (subdivisable) + { map.setCurrentLevel(fLevel) ; Algo::IHM::subdivideFace(map, old, position) ; subdiv = true ; @@ -217,16 +237,21 @@ void EnvMap::subdivideAllToMaxLevel() void EnvMap::subdivideToProperLevel() { bool subdiv ; - do { + do + { subdiv = false ; { CellMarker subd(map, FACE) ; - for (Dart d = map.begin() ; d != map.end() ; map.next(d)) { - if ( !subd.isMarked(d)) { + for (Dart d = map.begin(); d != map.end(); map.next(d)) + { + if (!subd.isMarked(d)) + { subd.mark(d) ; - if ( !refineMark.isMarked(d) && agentvect[d].size() > nbAgentsToSubdivide) { + if (!refineMark.isMarked(d) && agentvect[d].size() > nbAgentsToSubdivide) + { std::pair& sf = subdivisableFace[d] ; - if (sf.first == false || (sf.first == true && sf.second)) { + if (sf.first == false || (sf.first == true && sf.second)) + { subdiv = true ; refineMark.mark(d) ; refineCandidate.push_back(d) ; @@ -245,12 +270,13 @@ void EnvMap::subdivideToProperLevel() Dart EnvMap::getBelongingCell(const PFP::VEC3& pos) { CellMarkerStore m(map, FACE) ; - for (Dart d = map.begin() ; d != map.end() ; map.next(d)) { - if ( !m.isMarked(d)) { + for (Dart d = map.begin(); d != map.end(); map.next(d)) + { + if (!m.isMarked(d)) + { m.mark(d) ; - if ( !map.isBoundaryFace(d) && !buildingMark.isMarked(d) - && Algo::Geometry::isPointInConvexFace2D(map, d, position, pos, true)) - return d ; + if (!map.isBoundaryFace(d) && !buildingMark.isMarked(d) + && Algo::Geometry::isPointInConvexFace2D(map, d, position, pos, true)) return d ; } } @@ -263,9 +289,11 @@ Dart EnvMap::getBelongingCell(const PFP::VEC3& pos) void EnvMap::foreach_neighborFace(Dart d, FunctorType& f) { Dart dd = d ; - do { + do + { Dart ddd = map.alpha1(map.alpha1(dd)) ; - while (ddd != dd) { + while (ddd != dd) + { f(ddd) ; ddd = map.alpha1(ddd) ; } @@ -276,33 +304,40 @@ void EnvMap::foreach_neighborFace(Dart d, FunctorType& f) void EnvMap::registerObstaclesInFaces() { CellMarker m(map, FACE) ; - for (Dart d = map.begin() ; d != map.end() ; map.next(d)) { - if ( !m.isMarked(d)) { + for (Dart d = map.begin(); d != map.end(); map.next(d)) + { + if (!m.isMarked(d)) + { m.mark(d) ; - if ( !buildingMark.isMarked(d)) { + if (!buildingMark.isMarked(d)) + { Dart dd = d ; //retrieve all obstacles within its one-ring //first : test all edges of the face of d - do { - if (obstacleMark.isMarked(dd)) { + do + { + if (obstacleMark.isMarked(dd)) + { Dart dd2 = map.phi2(dd) ; Dart next = map.phi1(dd2) ; Dart previous = map.phi_1(dd2) ; Obstacle* o = new Obstacle(position[dd2], position[next], - position[previous], position[map.phi1(next)]) ; + position[previous], position[map.phi1(next)]) ; obstvect[d].push_back(o) ; } dd = map.phi1(dd) ; } while (dd != d) ; //second : test all edges of neighboring faces - do { + do + { Dart ddd = map.alpha1(map.alpha1(dd)) ; - while (ddd != dd) { - if ( !buildingMark.isMarked(ddd)) - addNeighborObstacles(obstvect[d], ddd, ddd == map.alpha_1(dd)) ; + while (ddd != dd) + { + if (!buildingMark.isMarked(ddd)) addNeighborObstacles( + obstvect[d], ddd, ddd == map.alpha_1(dd)) ; ddd = map.alpha1(ddd) ; } dd = map.phi1(dd) ; @@ -315,12 +350,16 @@ void EnvMap::registerObstaclesInFaces() void EnvMap::addNeighborObstacles(PFP::OBSTACLES& obst, Dart d, bool edgeNeighbor) { Dart stop ; - if (edgeNeighbor) stop = map.phi_1(map.phi_1(map.phi_1(d))) ; - else stop = map.phi_1(map.phi_1(d)) ; + if (edgeNeighbor) + stop = map.phi_1(map.phi_1(map.phi_1(d))) ; + else + stop = map.phi_1(map.phi_1(d)) ; Dart dd = d ; - do { - if (obstacleMark.isMarked(dd)) { + do + { + if (obstacleMark.isMarked(dd)) + { // if(buildingMark.isMarked(dd)) // { // std::cout << "caca prout" << std::endl; @@ -335,7 +374,7 @@ void EnvMap::addNeighborObstacles(PFP::OBSTACLES& obst, Dart d, bool edgeNeighbo Dart next = map.phi1(dd2) ; Dart previous = map.phi_1(dd2) ; Obstacle* o = new Obstacle(position[dd2], position[next], position[previous], - position[map.phi1(next)]) ; + position[map.phi1(next)]) ; obst.push_back(o) ; // } } @@ -428,16 +467,19 @@ void EnvMap::agentChangeFace(Agent* agent, Dart oldFace) popAgentInCells(agent, oldFace) ; pushAgentInCells(agent, newFace) ; - if ( !refineMark.isMarked(newFace) && agentvect[newFace].size() > nbAgentsToSubdivide) { + if (!refineMark.isMarked(newFace) && agentvect[newFace].size() > nbAgentsToSubdivide) + { std::pair& sf = subdivisableFace[newFace] ; - if (sf.first == false || (sf.first == true && sf.second)) { + if (sf.first == false || (sf.first == true && sf.second)) + { refineMark.mark(newFace) ; refineCandidate.push_back(newFace) ; } } - if ( !coarsenMark.isMarked(oldFace) && !refineMark.isMarked(oldFace) - && agentvect[oldFace].size() < nbAgentsToSimplify) { + if (!coarsenMark.isMarked(oldFace) && !refineMark.isMarked(oldFace) + && 4 * agentvect[oldFace].size() < nbAgentsToSimplify) + { coarsenMark.mark(oldFace) ; coarsenCandidate.push_back(map.faceOldestDart(oldFace)) ; } @@ -453,32 +495,37 @@ void EnvMap::updateMap() { assert(map.getCurrentLevel() == map.getMaxLevel()) ; - for (std::vector::iterator it = refineCandidate.begin() ; - it != refineCandidate.end() ; ++it) { - Dart d = ( *it) ; + for (std::vector::iterator it = refineCandidate.begin(); it != refineCandidate.end(); + ++it) + { + Dart d = (*it) ; refineMark.unmark(d) ; - if (agentvect[d].size() > nbAgentsToSubdivide) { + if (agentvect[d].size() > nbAgentsToSubdivide) + { int fLevel = -1 ; Dart old = map.faceOldestDart(d) ; bool subdivisable = true ; std::pair& sf = subdivisableFace[old] ; - if (sf.first == true) subdivisable = sf.second ; - else { + if (sf.first == true) + subdivisable = sf.second ; + else + { float minSizeSq = minCellSize * minCellSize ; // diametre de vision de l'agent au carré fLevel = map.faceLevel(old) ; map.setCurrentLevel(fLevel) ; PFP::VEC3 fCenter = Algo::Geometry::faceCentroid(map, old, position) ; Dart fd = old ; - do { + do + { PFP::VEC3& p = position[fd] ; PFP::VEC3 edge = Algo::Geometry::vectorOutOfDart(map, fd, position) ; - PFP::VEC3 proj = fCenter - - (p + (edge * (fCenter - p) / edge.norm2()) * edge) ; - if (proj.norm2() < minSizeSq) { + PFP::VEC3 proj = fCenter - (p + (edge * (fCenter - p) / edge.norm2()) * edge) ; + if (proj.norm2() < minSizeSq) + { subdivisable = false ; break ; } @@ -490,15 +537,15 @@ void EnvMap::updateMap() sf.second = subdivisable ; } - if (subdivisable) { + if (subdivisable) + { if (fLevel == -1) fLevel = map.faceLevel(old) ; sf.first = false ; PFP::AGENTS oldAgents(agentvect[old]) ; - for (PFP::AGENTS::iterator ait = oldAgents.begin() ; ait != oldAgents.end() ; - ++ait) - popAgentInCells( *ait, old) ; + for (PFP::AGENTS::iterator ait = oldAgents.begin(); ait != oldAgents.end(); ++ait) + popAgentInCells(*ait, old) ; neighborAgentvect[old].clear() ; @@ -507,31 +554,37 @@ void EnvMap::updateMap() CellMarkerStore newF(map, FACE) ; unsigned int degree = 0 ; Dart dd = old ; - do { + do + { ++degree ; newF.mark(dd) ; dd = map.phi1(dd) ; } while (dd != old) ; - if (degree == 3) { + if (degree == 3) + { Dart centerFace = map.phi2(map.phi1(old)) ; newF.mark(centerFace) ; } map.setCurrentLevel(map.getMaxLevel()) ; - for (PFP::AGENTS::iterator ait = oldAgents.begin() ; ait != oldAgents.end() ; - ++ait) { - resetAgentInFace( *ait) ; - pushAgentInCells( *ait, ( *ait)->part_.d) ; + for (PFP::AGENTS::iterator ait = oldAgents.begin(); ait != oldAgents.end(); ++ait) + { + resetAgentInFace(*ait) ; + pushAgentInCells(*ait, (*ait)->part_.d) ; } dd = old ; - do { + do + { Dart d3 = dd ; - do { + do + { Dart d4 = map.alpha1(map.alpha1(d3)) ; PFP::AGENTS& nad3 = neighborAgentvect[d3] ; - while (d4 != d3) { - if ( !newF.isMarked(d4)) { + while (d4 != d3) + { + if (!newF.isMarked(d4)) + { PFP::AGENTS& ad4 = agentvect[d4] ; nad3.insert(nad3.end(), ad4.begin(), ad4.end()) ; } @@ -545,17 +598,20 @@ void EnvMap::updateMap() map.setCurrentLevel(map.getMaxLevel()) ; } while (dd != old) ; - if (degree == 3) { + if (degree == 3) + { Dart centerFace = map.phi2(map.phi1(old)) ; Dart d3 = centerFace ; - do { + do + { Dart d4 = map.alpha1(map.alpha1(d3)) ; PFP::AGENTS& nad3 = neighborAgentvect[d3] ; - while (d4 != d3) { - if ( !newF.isMarked(d4)) { + while (d4 != d3) + { + if (!newF.isMarked(d4)) + { PFP::AGENTS& ad4 = agentvect[d4] ; - nad3.insert(neighborAgentvect[d3].end(), ad4.begin(), - ad4.end()) ; + nad3.insert(neighborAgentvect[d3].end(), ad4.begin(), ad4.end()) ; } d4 = map.alpha1(d4) ; } @@ -565,129 +621,149 @@ void EnvMap::updateMap() } } } - - for (unsigned int it = 0 ; it < coarsenCandidate.size() ; ++it) { + refineCandidate.clear() ; + + // On recrée une liste des faces à simplifier en empêchant les doublons + // On en profite pour vérifier les conditions de simplifications + std::vector checkCoarsenCandidate ; + checkCoarsenCandidate.reserve(coarsenCandidate.size()) ; + for (unsigned int it = 0; it < coarsenCandidate.size(); ++it) + { Dart old = coarsenCandidate[it] ; + bool oldIsMarked = coarsenMark.isMarked(old) ; coarsenMark.unmark(old) ; unsigned int fLevel = map.faceLevel(old) ; - if (fLevel > 0 && map.getDartLevel(old) < fLevel) { + if (oldIsMarked && fLevel > 0 && map.getDartLevel(old) < fLevel) + { unsigned int cur = map.getCurrentLevel() ; map.setCurrentLevel(fLevel - 1) ; - if (map.faceIsSubdividedOnce(old)) { + if (map.faceIsSubdividedOnce(old)) + { // on compte le nombre d'agents dans les sous-faces // on en profite pour compter le degré de la face grossière unsigned int degree = 0 ; unsigned int nbAgents = 0 ; Dart fit = old ; - do { + do + { nbAgents += agentvect[fit].size() ; ++degree ; - coarsenMark.unmark(fit) ; - unsigned int start = it + 1 ; - unsigned int fEmb = map.getEmbedding(FACE, fit) ; - while (start < coarsenCandidate.size()) { - if (map.getEmbedding(FACE, coarsenCandidate[start]) == fEmb) { - coarsenCandidate[start] = coarsenCandidate.back() ; - coarsenCandidate.pop_back() ; - } - else ++start ; - } - fit = map.phi1(fit) ; } while (fit != old) ; - if (degree == 3) { + if (degree == 3) + { map.setCurrentLevel(fLevel) ; Dart centerFace = map.phi2(map.phi1(old)) ; nbAgents += agentvect[centerFace].size() ; coarsenMark.unmark(centerFace) ; - unsigned int start = it + 1 ; - unsigned int fEmb = map.getEmbedding(FACE, centerFace) ; - while (start < coarsenCandidate.size()) { - if (map.getEmbedding(FACE, coarsenCandidate[start]) == fEmb) { - coarsenCandidate[start] = coarsenCandidate.back() ; - coarsenCandidate.pop_back() ; - } - else ++start ; - } map.setCurrentLevel(fLevel - 1) ; } + if (nbAgents < nbAgentsToSimplify) checkCoarsenCandidate.push_back(old) ; + } + map.setCurrentLevel(cur) ; + } + } + coarsenCandidate.clear() ; - if (nbAgents < nbAgentsToSimplify) { - PFP::AGENTS agents ; - Dart fit = old ; - do { - PFP::AGENTS a(agentvect[fit]) ; - agents.insert(agents.end(), a.begin(), a.end()) ; - map.setCurrentLevel(map.getMaxLevel()) ; - for (PFP::AGENTS::iterator ait = a.begin() ; ait != a.end() ; ++ait) - popAgentInCells( *ait, fit) ; - map.setCurrentLevel(fLevel - 1) ; - Dart nf = map.phi2(fit) ; - if ( !map.faceIsSubdivided(nf)) { - map.setCurrentLevel(fLevel) ; - PFP::AGENTS& an = agentvect[nf] ; - for (PFP::AGENTS::iterator ait = an.begin() ; ait != an.end() ; - ++ait) { - if (( *ait)->part_.d == map.phi1(nf)) ( *ait)->part_.d = nf ; - } - map.setCurrentLevel(fLevel - 1) ; - } - fit = map.phi1(fit) ; - } while (fit != old) ; - if (degree == 3) { - map.setCurrentLevel(fLevel) ; - Dart centerFace = map.phi2(map.phi1(old)) ; - PFP::AGENTS a(agentvect[centerFace]) ; - agents.insert(agents.end(), a.begin(), a.end()) ; - map.setCurrentLevel(map.getMaxLevel()) ; - for (PFP::AGENTS::iterator ait = a.begin() ; ait != a.end() ; ++ait) - popAgentInCells( *ait, centerFace) ; - map.setCurrentLevel(fLevel - 1) ; - } - neighborAgentvect[old].clear() ; + // On réalise la simplification (les conditions ont déjà été vérifiées) + for (unsigned int it = 0; it < checkCoarsenCandidate.size(); ++it) + { + Dart old = checkCoarsenCandidate[it] ; - Algo::IHM::coarsenFace(map, old, position) ; + unsigned int fLevel = map.faceLevel(old) ; + unsigned int cur = map.getCurrentLevel() ; + map.setCurrentLevel(fLevel - 1) ; - std::pair& sf = subdivisableFace[old] ; - sf.first = true ; - sf.second = true ; + // on compte le degré de la face grossière + unsigned int degree = 0 ; + Dart fit = old ; + do + { + ++degree ; + fit = map.phi1(fit) ; + } while (fit != old) ; - map.setCurrentLevel(map.getMaxLevel()) ; + PFP::AGENTS agents ; + fit = old ; + do + { + PFP::AGENTS a(agentvect[fit]) ; + agents.insert(agents.end(), a.begin(), a.end()) ; + map.setCurrentLevel(map.getMaxLevel()) ; + for (PFP::AGENTS::iterator ait = a.begin(); ait != a.end(); ++ait) + popAgentInCells(*ait, fit) ; + map.setCurrentLevel(fLevel - 1) ; + Dart nf = map.phi2(fit) ; + if (!map.faceIsSubdivided(nf)) + { + map.setCurrentLevel(fLevel) ; + PFP::AGENTS& an = agentvect[nf] ; + for (PFP::AGENTS::iterator ait = an.begin(); ait != an.end(); ++ait) + { + if ((*ait)->part_.d == map.phi1(nf)) (*ait)->part_.d = nf ; + } + map.setCurrentLevel(fLevel - 1) ; + } + fit = map.phi1(fit) ; + } while (fit != old) ; - for (PFP::AGENTS::iterator itA = agents.begin() ; itA != agents.end() ; - ++itA) { - ( *itA)->part_.d = old ; - pushAgentInCells( *itA, old) ; - } + if (degree == 3) + { + map.setCurrentLevel(fLevel) ; + Dart centerFace = map.phi2(map.phi1(old)) ; + PFP::AGENTS a(agentvect[centerFace]) ; + agents.insert(agents.end(), a.begin(), a.end()) ; + map.setCurrentLevel(map.getMaxLevel()) ; + for (PFP::AGENTS::iterator ait = a.begin(); ait != a.end(); ++ait) + popAgentInCells(*ait, centerFace) ; + map.setCurrentLevel(fLevel - 1) ; + } + neighborAgentvect[old].clear() ; - Dart dd = old ; - do { - Dart ddd = map.alpha1(map.alpha1(dd)) ; - while (ddd != dd) { - neighborAgentvect[old].insert(neighborAgentvect[old].end(), - agentvect[ddd].begin(), agentvect[ddd].end()) ; - ddd = map.alpha1(ddd) ; - } - dd = map.phi1(dd) ; - } while (dd != old) ; + Algo::IHM::coarsenFace(map, old, position) ; - if (fLevel > 1 && !coarsenMark.isMarked(old) - && agentvect[old].size() < nbAgentsToSimplify) { - coarsenMark.mark(old) ; - coarsenCandidate.push_back(map.faceOldestDart(old)) ; - } - } + std::pair& sf = subdivisableFace[old] ; + sf.first = true ; + sf.second = true ; + + map.setCurrentLevel(map.getMaxLevel()) ; + + for (PFP::AGENTS::iterator itA = agents.begin(); itA != agents.end(); ++itA) + { + (*itA)->part_.d = old ; + pushAgentInCells(*itA, old) ; + } + + Dart dd = old ; + do + { + Dart ddd = map.alpha1(map.alpha1(dd)) ; + while (ddd != dd) + { + neighborAgentvect[old].insert(neighborAgentvect[old].end(), + agentvect[ddd].begin(), + agentvect[ddd].end()) ; + ddd = map.alpha1(ddd) ; } - map.setCurrentLevel(cur) ; + dd = map.phi1(dd) ; + } while (dd != old) ; + + if (fLevel > 1 && !coarsenMark.isMarked(old) + && agentvect[old].size() < nbAgentsToSimplify) + { + coarsenMark.mark(old) ; + coarsenCandidate.push_back(map.faceOldestDart(old)) ; } - } + map.setCurrentLevel(cur) ; + } map.setCurrentLevel(map.getMaxLevel()) ; + if (coarsenCandidate.size() > 0) updateMap() ; } void EnvMap::resetAgentInFace(Agent* agent) diff --git a/src/gl2ps.c b/src/gl2ps.c index def453b07996d07b767a918ab7397e900dc2e040..4fc327d9152f4c73360b4d10312b132c53e1cc74 100644 --- a/src/gl2ps.c +++ b/src/gl2ps.c @@ -110,156 +110,175 @@ #define GL2PS_DRAW_PIXELS_TOKEN 14 #define GL2PS_TEXT_TOKEN 15 -typedef enum { - T_UNDEFINED = -1, T_CONST_COLOR = 1, T_VAR_COLOR = 1 << 1, T_ALPHA_1 = 1 - << 2, T_ALPHA_LESS_1 = 1 << 3, T_VAR_ALPHA = 1 << 4 -} GL2PS_TRIANGLE_PROPERTY; - -typedef GLfloat GL2PSxyz[3]; -typedef GLfloat GL2PSplane[4]; - -typedef struct _GL2PSbsptree2d GL2PSbsptree2d; - -struct _GL2PSbsptree2d { - GL2PSplane plane; - GL2PSbsptree2d *front, *back; -}; - -typedef struct { - GLint nmax, size, incr, n; - char *array; -} GL2PSlist; - -typedef struct _GL2PSbsptree GL2PSbsptree; - -struct _GL2PSbsptree { - GL2PSplane plane; - GL2PSlist *primitives; - GL2PSbsptree *front, *back; -}; - -typedef struct { - GL2PSxyz xyz; - GL2PSrgba rgba; -} GL2PSvertex; - -typedef struct { - GL2PSvertex vertex[3]; - int prop; -} GL2PStriangle; - -typedef struct { - GLshort fontsize; - char *str, *fontname; +typedef enum +{ + T_UNDEFINED = -1, + T_CONST_COLOR = 1, + T_VAR_COLOR = 1 << 1, + T_ALPHA_1 = 1 << 2, + T_ALPHA_LESS_1 = 1 << 3, + T_VAR_ALPHA = 1 << 4 +} GL2PS_TRIANGLE_PROPERTY ; + +typedef GLfloat GL2PSxyz[3] ; +typedef GLfloat GL2PSplane[4] ; + +typedef struct _GL2PSbsptree2d GL2PSbsptree2d ; + +struct _GL2PSbsptree2d +{ + GL2PSplane plane ; + GL2PSbsptree2d *front, *back ; +} ; + +typedef struct +{ + GLint nmax, size, incr, n ; + char *array ; +} GL2PSlist ; + +typedef struct _GL2PSbsptree GL2PSbsptree ; + +struct _GL2PSbsptree +{ + GL2PSplane plane ; + GL2PSlist *primitives ; + GL2PSbsptree *front, *back ; +} ; + +typedef struct +{ + GL2PSxyz xyz ; + GL2PSrgba rgba ; +} GL2PSvertex ; + +typedef struct +{ + GL2PSvertex vertex[3] ; + int prop ; +} GL2PStriangle ; + +typedef struct +{ + GLshort fontsize ; + char *str, *fontname ; /* Note: for a 'special' string, 'alignment' holds the format (PostScript, PDF, etc.) of the special string */ - GLint alignment; - GLfloat angle; -} GL2PSstring; + GLint alignment ; + GLfloat angle ; +} GL2PSstring ; -typedef struct { - GLsizei width, height; +typedef struct +{ + GLsizei width, height ; /* Note: for an imagemap, 'type' indicates if it has already been written to the file or not, and 'format' indicates if it is visible or not */ - GLenum format, type; - GLfloat *pixels; -} GL2PSimage; - -typedef struct _GL2PSimagemap GL2PSimagemap; - -struct _GL2PSimagemap { - GL2PSimage *image; - GL2PSimagemap *next; -}; - -typedef struct { - GLshort type, numverts; - GLushort pattern; - char boundary, offset, culled; - GLint factor; - GLfloat width; - GL2PSvertex *verts; - union { - GL2PSstring *text; - GL2PSimage *image; - } data; -} GL2PSprimitive; - -typedef struct { + GLenum format, type ; + GLfloat *pixels ; +} GL2PSimage ; + +typedef struct _GL2PSimagemap GL2PSimagemap ; + +struct _GL2PSimagemap +{ + GL2PSimage *image ; + GL2PSimagemap *next ; +} ; + +typedef struct +{ + GLshort type, numverts ; + GLushort pattern ; + char boundary, offset, culled ; + GLint factor ; + GLfloat width ; + GL2PSvertex *verts ; + union + { + GL2PSstring *text ; + GL2PSimage *image ; + } data ; +} GL2PSprimitive ; + +typedef struct +{ #if defined(GL2PS_HAVE_ZLIB) - Bytef *dest, *src, *start; - uLongf destLen, srcLen; + Bytef *dest, *src, *start ; + uLongf destLen, srcLen ; #else - int dummy; + int dummy ; #endif -} GL2PScompress; +} GL2PScompress ; -typedef struct { - GL2PSlist* ptrlist; - int gsno, fontno, imno, shno, maskshno, trgroupno; - int gsobjno, fontobjno, imobjno, shobjno, maskshobjno, trgroupobjno; -} GL2PSpdfgroup; +typedef struct +{ + GL2PSlist* ptrlist ; + int gsno, fontno, imno, shno, maskshno, trgroupno ; + int gsobjno, fontobjno, imobjno, shobjno, maskshobjno, trgroupobjno ; +} GL2PSpdfgroup ; -typedef struct { +typedef struct +{ /* General */ - GLint format, sort, options, colorsize, colormode, buffersize; - char *title, *producer, *filename; - GLboolean boundary, blending; - GLfloat *feedback, offset[2], lastlinewidth; - GLint viewport[4], blendfunc[2], lastfactor; - GL2PSrgba *colormap, lastrgba, threshold, bgcolor; - GLushort lastpattern; - GL2PSvertex lastvertex; - GL2PSlist *primitives, *auxprimitives; - FILE *stream; - GL2PScompress *compress; - GLboolean header; + GLint format, sort, options, colorsize, colormode, buffersize ; + char *title, *producer, *filename ; + GLboolean boundary, blending ; + GLfloat *feedback, offset[2], lastlinewidth ; + GLint viewport[4], blendfunc[2], lastfactor ; + GL2PSrgba *colormap, lastrgba, threshold, bgcolor ; + GLushort lastpattern ; + GL2PSvertex lastvertex ; + GL2PSlist *primitives, *auxprimitives ; + FILE *stream ; + GL2PScompress *compress ; + GLboolean header ; /* BSP-specific */ - GLint maxbestroot; + GLint maxbestroot ; /* Occlusion culling-specific */ - GLboolean zerosurfacearea; - GL2PSbsptree2d *imagetree; - GL2PSprimitive *primitivetoadd; + GLboolean zerosurfacearea ; + GL2PSbsptree2d *imagetree ; + GL2PSprimitive *primitivetoadd ; /* PDF-specific */ - int streamlength; - GL2PSlist *pdfprimlist, *pdfgrouplist; - int *xreflist; - int objects_stack; /* available objects */ - int extgs_stack; /* graphics state object number */ - int font_stack; /* font object number */ - int im_stack; /* image object number */ - int trgroupobjects_stack; /* xobject numbers */ - int shader_stack; /* shader object numbers */ - int mshader_stack; /* mask shader object numbers */ + int streamlength ; + GL2PSlist *pdfprimlist, *pdfgrouplist ; + int *xreflist ; + int objects_stack ; /* available objects */ + int extgs_stack ; /* graphics state object number */ + int font_stack ; /* font object number */ + int im_stack ; /* image object number */ + int trgroupobjects_stack ; /* xobject numbers */ + int shader_stack ; /* shader object numbers */ + int mshader_stack ; /* mask shader object numbers */ /* for image map list */ - GL2PSimagemap *imagemap_head; - GL2PSimagemap *imagemap_tail; -} GL2PScontext; - -typedef struct { - void (*printHeader)(void); - void (*printFooter)(void); - void (*beginViewport)(GLint viewport[4]); - GLint (*endViewport)(void); - void (*printPrimitive)(void *data); - void (*printFinalPrimitive)(void); - const char *file_extension; - const char *description; -} GL2PSbackend; + GL2PSimagemap *imagemap_head ; + GL2PSimagemap *imagemap_tail ; +} GL2PScontext ; + +typedef struct +{ + void (*printHeader)(void) ; + void (*printFooter)(void) ; + void (*beginViewport)(GLint viewport[4]) ; + GLint (*endViewport)(void) ; + void (*printPrimitive)(void *data) ; + void (*printFinalPrimitive)(void) ; + const char *file_extension ; + const char *description ; +} GL2PSbackend ; /* The gl2ps context. gl2ps is not thread safe (we should create a local GL2PScontext during gl2psBeginPage) */ -static GL2PScontext *gl2ps = NULL; +static GL2PScontext *gl2ps = NULL ; /* Need to forward-declare this one */ -static GLint gl2psPrintPrimitives(void); +static GLint gl2psPrintPrimitives(void) ; /********************************************************************* * @@ -267,66 +286,73 @@ static GLint gl2psPrintPrimitives(void); * *********************************************************************/ -static void gl2psMsg(GLint level, const char *fmt, ...) { - va_list args; - - if (!(gl2ps->options & GL2PS_SILENT)) { - switch (level) { - case GL2PS_INFO: - fprintf(stderr, "GL2PS info: "); - break; - case GL2PS_WARNING: - fprintf(stderr, "GL2PS warning: "); - break; - case GL2PS_ERROR: - fprintf(stderr, "GL2PS error: "); - break; +static void gl2psMsg(GLint level, const char *fmt, ...) +{ + va_list args ; + + if (!(gl2ps->options & GL2PS_SILENT)) + { + switch (level) + { + case GL2PS_INFO : + fprintf(stderr, "GL2PS info: ") ; + break ; + case GL2PS_WARNING : + fprintf(stderr, "GL2PS warning: ") ; + break ; + case GL2PS_ERROR : + fprintf(stderr, "GL2PS error: ") ; + break ; } - va_start(args, fmt); - vfprintf(stderr, fmt, args); - va_end(args); - fprintf(stderr, "\n"); + va_start(args, fmt) ; + vfprintf(stderr, fmt, args) ; + va_end(args) ; + fprintf(stderr, "\n") ; } /* if(level == GL2PS_ERROR) exit(1); */ } -static void *gl2psMalloc(size_t size) { - void *ptr; +static void *gl2psMalloc(size_t size) +{ + void *ptr ; - if (!size) - return NULL; - ptr = malloc(size); - if (!ptr) { - gl2psMsg(GL2PS_ERROR, "Couldn't allocate requested memory"); - return NULL; + if (!size) return NULL ; + ptr = malloc(size) ; + if (!ptr) + { + gl2psMsg(GL2PS_ERROR, "Couldn't allocate requested memory") ; + return NULL ; } - return ptr; + return ptr ; } -static void *gl2psRealloc(void *ptr, size_t size) { - if (!size) - return NULL; - ptr = realloc(ptr, size); - if (!ptr) { - gl2psMsg(GL2PS_ERROR, "Couldn't reallocate requested memory"); - return NULL; +static void *gl2psRealloc(void *ptr, size_t size) +{ + if (!size) return NULL ; + ptr = realloc(ptr, size) ; + if (!ptr) + { + gl2psMsg(GL2PS_ERROR, "Couldn't reallocate requested memory") ; + return NULL ; } - return ptr; + return ptr ; } -static void gl2psFree(void *ptr) { - if (!ptr) - return; - free(ptr); +static void gl2psFree(void *ptr) +{ + if (!ptr) return ; + free(ptr) ; } -static size_t gl2psWriteBigEndian(unsigned long data, size_t bytes) { - size_t i; - size_t size = sizeof(unsigned long); - for (i = 1; i <= bytes; ++i) { - fputc(0xff & (data >> (size - i) * 8), gl2ps->stream); +static size_t gl2psWriteBigEndian(unsigned long data, size_t bytes) +{ + size_t i ; + size_t size = sizeof(unsigned long) ; + for (i = 1 ; i <= bytes; ++i) + { + fputc(0xff & (data >> (size - i) * 8), gl2ps->stream) ; } - return bytes; + return bytes ; } /* zlib compression helper routines */ @@ -335,71 +361,72 @@ static size_t gl2psWriteBigEndian(unsigned long data, size_t bytes) { static void gl2psSetupCompress(void) { - gl2ps->compress = (GL2PScompress*)gl2psMalloc(sizeof(GL2PScompress)); - gl2ps->compress->src = NULL; - gl2ps->compress->start = NULL; - gl2ps->compress->dest = NULL; - gl2ps->compress->srcLen = 0; - gl2ps->compress->destLen = 0; + gl2ps->compress = (GL2PScompress*)gl2psMalloc(sizeof(GL2PScompress)) ; + gl2ps->compress->src = NULL ; + gl2ps->compress->start = NULL ; + gl2ps->compress->dest = NULL ; + gl2ps->compress->srcLen = 0 ; + gl2ps->compress->destLen = 0 ; } static void gl2psFreeCompress(void) { if(!gl2ps->compress) - return; - gl2psFree(gl2ps->compress->start); - gl2psFree(gl2ps->compress->dest); - gl2ps->compress->src = NULL; - gl2ps->compress->start = NULL; - gl2ps->compress->dest = NULL; - gl2ps->compress->srcLen = 0; - gl2ps->compress->destLen = 0; + return ; + gl2psFree(gl2ps->compress->start) ; + gl2psFree(gl2ps->compress->dest) ; + gl2ps->compress->src = NULL ; + gl2ps->compress->start = NULL ; + gl2ps->compress->dest = NULL ; + gl2ps->compress->srcLen = 0 ; + gl2ps->compress->destLen = 0 ; } static int gl2psAllocCompress(unsigned int srcsize) { - gl2psFreeCompress(); + gl2psFreeCompress() ; if(!gl2ps->compress || !srcsize) - return GL2PS_ERROR; + return GL2PS_ERROR ; - gl2ps->compress->srcLen = srcsize; - gl2ps->compress->destLen = (int)ceil(1.001 * gl2ps->compress->srcLen + 12); - gl2ps->compress->src = (Bytef*)gl2psMalloc(gl2ps->compress->srcLen); - gl2ps->compress->start = gl2ps->compress->src; - gl2ps->compress->dest = (Bytef*)gl2psMalloc(gl2ps->compress->destLen); + gl2ps->compress->srcLen = srcsize ; + gl2ps->compress->destLen = (int)ceil(1.001 * gl2ps->compress->srcLen + 12) ; + gl2ps->compress->src = (Bytef*)gl2psMalloc(gl2ps->compress->srcLen) ; + gl2ps->compress->start = gl2ps->compress->src ; + gl2ps->compress->dest = (Bytef*)gl2psMalloc(gl2ps->compress->destLen) ; - return GL2PS_SUCCESS; + return GL2PS_SUCCESS ; } static void *gl2psReallocCompress(unsigned int srcsize) { if(!gl2ps->compress || !srcsize) - return NULL; + return NULL ; if(srcsize < gl2ps->compress->srcLen) - return gl2ps->compress->start; + return gl2ps->compress->start ; - gl2ps->compress->srcLen = srcsize; - gl2ps->compress->destLen = (int)ceil(1.001 * gl2ps->compress->srcLen + 12); + gl2ps->compress->srcLen = srcsize ; + gl2ps->compress->destLen = (int)ceil(1.001 * gl2ps->compress->srcLen + 12) ; gl2ps->compress->src = (Bytef*)gl2psRealloc(gl2ps->compress->src, - gl2ps->compress->srcLen); - gl2ps->compress->start = gl2ps->compress->src; + gl2ps->compress->srcLen) ; + gl2ps->compress->start = gl2ps->compress->src ; gl2ps->compress->dest = (Bytef*)gl2psRealloc(gl2ps->compress->dest, - gl2ps->compress->destLen); + gl2ps->compress->destLen) ; - return gl2ps->compress->start; + return gl2ps->compress->start ; } static size_t gl2psWriteBigEndianCompress(unsigned long data, size_t bytes) { - size_t i; - size_t size = sizeof(unsigned long); - for(i = 1; i <= bytes; ++i) { - *gl2ps->compress->src = (Bytef)(0xff & (data >> (size-i) * 8)); - ++gl2ps->compress->src; + size_t i ; + size_t size = sizeof(unsigned long) ; + for(i = 1 ; i <= bytes ; ++i) + { + *gl2ps->compress->src = (Bytef)(0xff & (data >> (size-i) * 8)) ; + ++gl2ps->compress->src ; } - return bytes; + return bytes ; } static int gl2psDeflate(void) @@ -407,193 +434,217 @@ static int gl2psDeflate(void) /* For compatibility with older zlib versions, we use compress(...) instead of compress2(..., Z_BEST_COMPRESSION) */ return compress(gl2ps->compress->dest, &gl2ps->compress->destLen, - gl2ps->compress->start, gl2ps->compress->srcLen); + gl2ps->compress->start, gl2ps->compress->srcLen) ; } #endif -static int gl2psPrintf(const char* fmt, ...) { - int ret; - va_list args; +static int gl2psPrintf(const char* fmt, ...) +{ + int ret ; + va_list args ; #if defined(GL2PS_HAVE_ZLIB) - unsigned int oldsize = 0; - static char buf[1000]; - if(gl2ps->options & GL2PS_COMPRESS) { - va_start(args, fmt); - ret = vsprintf(buf, fmt, args); - va_end(args); - oldsize = gl2ps->compress->srcLen; - gl2ps->compress->start = (Bytef*)gl2psReallocCompress(oldsize + ret); - memcpy(gl2ps->compress->start+oldsize, buf, ret); - ret = 0; - } - else { + unsigned int oldsize = 0 ; + static char buf[1000] ; + if(gl2ps->options & GL2PS_COMPRESS) + { + va_start(args, fmt) ; + ret = vsprintf(buf, fmt, args) ; + va_end(args) ; + oldsize = gl2ps->compress->srcLen ; + gl2ps->compress->start = (Bytef*)gl2psReallocCompress(oldsize + ret) ; + memcpy(gl2ps->compress->start+oldsize, buf, ret) ; + ret = 0 ; + } + else + { #endif - va_start(args, fmt); - ret = vfprintf(gl2ps->stream, fmt, args); - va_end(args); + va_start(args, fmt) ; + ret = vfprintf(gl2ps->stream, fmt, args) ; + va_end(args) ; #if defined(GL2PS_HAVE_ZLIB) } #endif - return ret; + return ret ; } -static void gl2psPrintGzipHeader() { +static void gl2psPrintGzipHeader() +{ #if defined(GL2PS_HAVE_ZLIB) - char tmp[10] = {'\x1f', '\x8b', /* magic numbers: 0x1f, 0x8b */ + char tmp[10] = + { '\x1f', '\x8b', /* magic numbers: 0x1f, 0x8b */ 8, /* compression method: Z_DEFLATED */ 0, /* flags */ 0, 0, 0, 0, /* time */ 2, /* extra flags: max compression */ - '\x03'}; /* OS code: 0x03 (Unix) */ + '\x03'} ; /* OS code: 0x03 (Unix) */ - if(gl2ps->options & GL2PS_COMPRESS) { - gl2psSetupCompress(); + if(gl2ps->options & GL2PS_COMPRESS) + { + gl2psSetupCompress() ; /* add the gzip file header */ - fwrite(tmp, 10, 1, gl2ps->stream); + fwrite(tmp, 10, 1, gl2ps->stream) ; } #endif } -static void gl2psPrintGzipFooter() { +static void gl2psPrintGzipFooter() +{ #if defined(GL2PS_HAVE_ZLIB) - int n; - uLong crc, len; - char tmp[8]; + int n ; + uLong crc, len ; + char tmp[8] ; - if(gl2ps->options & GL2PS_COMPRESS) { - if(Z_OK != gl2psDeflate()) { - gl2psMsg(GL2PS_ERROR, "Zlib deflate error"); + if(gl2ps->options & GL2PS_COMPRESS) + { + if(Z_OK != gl2psDeflate()) + { + gl2psMsg(GL2PS_ERROR, "Zlib deflate error") ; } - else { + else + { /* determine the length of the header in the zlib stream */ - n = 2; /* CMF+FLG */ - if(gl2ps->compress->dest[1] & (1<<5)) { - n += 4; /* DICTID */ + n = 2 ; /* CMF+FLG */ + if(gl2ps->compress->dest[1] & (1<<5)) + { + n += 4 ; /* DICTID */ } /* write the data, without the zlib header and footer */ fwrite(gl2ps->compress->dest+n, gl2ps->compress->destLen-(n+4), - 1, gl2ps->stream); + 1, gl2ps->stream) ; /* add the gzip file footer */ - crc = crc32(0L, gl2ps->compress->start, gl2ps->compress->srcLen); - for(n = 0; n < 4; ++n) { - tmp[n] = (char)(crc & 0xff); - crc >>= 8; + crc = crc32(0L, gl2ps->compress->start, gl2ps->compress->srcLen) ; + for(n = 0 ; n < 4 ; ++n) + { + tmp[n] = (char)(crc & 0xff) ; + crc >>= 8 ; } - len = gl2ps->compress->srcLen; - for(n = 4; n < 8; ++n) { - tmp[n] = (char)(len & 0xff); - len >>= 8; + len = gl2ps->compress->srcLen ; + for(n = 4 ; n < 8 ; ++n) + { + tmp[n] = (char)(len & 0xff) ; + len >>= 8 ; } - fwrite(tmp, 8, 1, gl2ps->stream); + fwrite(tmp, 8, 1, gl2ps->stream) ; } - gl2psFreeCompress(); - gl2psFree(gl2ps->compress); - gl2ps->compress = NULL; + gl2psFreeCompress() ; + gl2psFree(gl2ps->compress) ; + gl2ps->compress = NULL ; } #endif } /* The list handling routines */ -static void gl2psListRealloc(GL2PSlist *list, GLint n) { - if (!list) { - gl2psMsg(GL2PS_ERROR, "Cannot reallocate NULL list"); - return; - } - if (n <= 0) - return; - if (!list->array) { - list->nmax = n; - list->array = (char*) gl2psMalloc(list->nmax * list->size); - } else { - if (n > list->nmax) { - list->nmax = ((n - 1) / list->incr + 1) * list->incr; - list->array = (char*) gl2psRealloc(list->array, - list->nmax * list->size); +static void gl2psListRealloc(GL2PSlist *list, GLint n) +{ + if (!list) + { + gl2psMsg(GL2PS_ERROR, "Cannot reallocate NULL list") ; + return ; + } + if (n <= 0) return ; + if (!list->array) + { + list->nmax = n ; + list->array = (char*)gl2psMalloc(list->nmax * list->size) ; + } + else + { + if (n > list->nmax) + { + list->nmax = ((n - 1) / list->incr + 1) * list->incr ; + list->array = (char*)gl2psRealloc(list->array, list->nmax * list->size) ; } } } -static GL2PSlist *gl2psListCreate(GLint n, GLint incr, GLint size) { - GL2PSlist *list; +static GL2PSlist *gl2psListCreate(GLint n, GLint incr, GLint size) +{ + GL2PSlist *list ; - if (n < 0) - n = 0; - if (incr <= 0) - incr = 1; - list = (GL2PSlist*) gl2psMalloc(sizeof(GL2PSlist)); - list->nmax = 0; - list->incr = incr; - list->size = size; - list->n = 0; - list->array = NULL; - gl2psListRealloc(list, n); - return list; + if (n < 0) n = 0 ; + if (incr <= 0) incr = 1 ; + list = (GL2PSlist*)gl2psMalloc(sizeof(GL2PSlist)) ; + list->nmax = 0 ; + list->incr = incr ; + list->size = size ; + list->n = 0 ; + list->array = NULL ; + gl2psListRealloc(list, n) ; + return list ; } -static void gl2psListReset(GL2PSlist *list) { - if (!list) - return; - list->n = 0; +static void gl2psListReset(GL2PSlist *list) +{ + if (!list) return ; + list->n = 0 ; } -static void gl2psListDelete(GL2PSlist *list) { - if (!list) - return; - gl2psFree(list->array); - gl2psFree(list); +static void gl2psListDelete(GL2PSlist *list) +{ + if (!list) return ; + gl2psFree(list->array) ; + gl2psFree(list) ; } -static void gl2psListAdd(GL2PSlist *list, void *data) { - if (!list) { - gl2psMsg(GL2PS_ERROR, "Cannot add into unallocated list"); - return; +static void gl2psListAdd(GL2PSlist *list, void *data) +{ + if (!list) + { + gl2psMsg(GL2PS_ERROR, "Cannot add into unallocated list") ; + return ; } - list->n++; - gl2psListRealloc(list, list->n); - memcpy(&list->array[(list->n - 1) * list->size], data, list->size); + list->n++ ; + gl2psListRealloc(list, list->n) ; + memcpy(&list->array[(list->n - 1) * list->size], data, list->size) ; } -static int gl2psListNbr(GL2PSlist *list) { - if (!list) - return 0; - return list->n; +static int gl2psListNbr(GL2PSlist *list) +{ + if (!list) return 0 ; + return list->n ; } -static void *gl2psListPointer(GL2PSlist *list, GLint index) { - if (!list) { - gl2psMsg(GL2PS_ERROR, "Cannot point into unallocated list"); - return NULL; +static void *gl2psListPointer(GL2PSlist *list, GLint index) +{ + if (!list) + { + gl2psMsg(GL2PS_ERROR, "Cannot point into unallocated list") ; + return NULL ; } - if ((index < 0) || (index >= list->n)) { - gl2psMsg(GL2PS_ERROR, "Wrong list index in gl2psListPointer"); - return NULL; + if ((index < 0) || (index >= list->n)) + { + gl2psMsg(GL2PS_ERROR, "Wrong list index in gl2psListPointer") ; + return NULL ; } - return &list->array[index * list->size]; + return &list->array[index * list->size] ; } -static void gl2psListSort(GL2PSlist *list, - int(*fcmp)(const void *a, const void *b)) { - if (!list) - return; - qsort(list->array, list->n, list->size, fcmp); +static void gl2psListSort(GL2PSlist *list, int(*fcmp)(const void *a, const void *b)) +{ + if (!list) return ; + qsort(list->array, list->n, list->size, fcmp) ; } -static void gl2psListAction(GL2PSlist *list, void(*action)(void *data)) { - GLint i; +static void gl2psListAction(GL2PSlist *list, void(*action)(void *data)) +{ + GLint i ; - for (i = 0; i < gl2psListNbr(list); i++) { - (*action)(gl2psListPointer(list, i)); + for (i = 0 ; i < gl2psListNbr(list); i++) + { + (*action)(gl2psListPointer(list, i)) ; } } -static void gl2psListActionInverse(GL2PSlist *list, void(*action)(void *data)) { - GLint i; +static void gl2psListActionInverse(GL2PSlist *list, void(*action)(void *data)) +{ + GLint i ; - for (i = gl2psListNbr(list); i > 0; i--) { - (*action)(gl2psListPointer(list, i - 1)); + for (i = gl2psListNbr(list) ; i > 0; i--) + { + (*action)(gl2psListPointer(list, i - 1)) ; } } @@ -602,159 +653,172 @@ static void gl2psListActionInverse(GL2PSlist *list, void(*action)(void *data)) { static void gl2psListRead(GL2PSlist *list, int index, void *data) { if((index < 0) || (index >= list->n)) - gl2psMsg(GL2PS_ERROR, "Wrong list index in gl2psListRead"); - memcpy(data, &list->array[index * list->size], list->size); + gl2psMsg(GL2PS_ERROR, "Wrong list index in gl2psListRead") ; + memcpy(data, &list->array[index * list->size], list->size) ; } static void gl2psEncodeBase64Block(unsigned char in[3], unsigned char out[4], int len) { static const char cb64[] = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ; - out[0] = cb64[ in[0] >> 2 ]; - out[1] = cb64[ ((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4) ]; - out[2] = (len > 1) ? cb64[ ((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6) ] : '='; - out[3] = (len > 2) ? cb64[ in[2] & 0x3f ] : '='; + out[0] = cb64[ in[0] >> 2 ] ; + out[1] = cb64[ ((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4) ] ; + out[2] = (len > 1) ? cb64[ ((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6) ] : '=' ; + out[3] = (len > 2) ? cb64[ in[2] & 0x3f ] : '=' ; } static void gl2psListEncodeBase64(GL2PSlist *list) { - unsigned char *buffer, in[3], out[4]; - int i, n, index, len; - - n = list->n * list->size; - buffer = (unsigned char*)gl2psMalloc(n * sizeof(unsigned char)); - memcpy(buffer, list->array, n * sizeof(unsigned char)); - gl2psListReset(list); - - index = 0; - while(index < n) { - len = 0; - for(i = 0; i < 3; i++) { - if(index < n) { - in[i] = buffer[index]; - len++; + unsigned char *buffer, in[3], out[4] ; + int i, n, index, len ; + + n = list->n * list->size ; + buffer = (unsigned char*)gl2psMalloc(n * sizeof(unsigned char)) ; + memcpy(buffer, list->array, n * sizeof(unsigned char)) ; + gl2psListReset(list) ; + + index = 0 ; + while(index < n) + { + len = 0 ; + for(i = 0 ; i < 3 ; i++) + { + if(index < n) + { + in[i] = buffer[index] ; + len++ ; } - else { - in[i] = 0; + else + { + in[i] = 0 ; } - index++; + index++ ; } - if(len) { - gl2psEncodeBase64Block(in, out, len); - for(i = 0; i < 4; i++) - gl2psListAdd(list, &out[i]); + if(len) + { + gl2psEncodeBase64Block(in, out, len) ; + for(i = 0 ; i < 4 ; i++) + gl2psListAdd(list, &out[i]) ; } } - gl2psFree(buffer); + gl2psFree(buffer) ; } #endif /* Helpers for rgba colors */ -static GLboolean gl2psSameColor(GL2PSrgba rgba1, GL2PSrgba rgba2) { +static GLboolean gl2psSameColor(GL2PSrgba rgba1, GL2PSrgba rgba2) +{ if (!GL2PS_ZERO(rgba1[0] - rgba2[0]) || !GL2PS_ZERO(rgba1[1] - rgba2[1]) - || !GL2PS_ZERO(rgba1[2] - rgba2[2])) - return GL_FALSE; - return GL_TRUE; + || !GL2PS_ZERO(rgba1[2] - rgba2[2])) return GL_FALSE ; + return GL_TRUE ; } -static GLboolean gl2psVertsSameColor(const GL2PSprimitive *prim) { - int i; +static GLboolean gl2psVertsSameColor(const GL2PSprimitive *prim) +{ + int i ; - for (i = 1; i < prim->numverts; i++) { - if (!gl2psSameColor(prim->verts[0].rgba, prim->verts[i].rgba)) { - return GL_FALSE; + for (i = 1 ; i < prim->numverts; i++) + { + if (!gl2psSameColor(prim->verts[0].rgba, prim->verts[i].rgba)) + { + return GL_FALSE ; } } - return GL_TRUE; + return GL_TRUE ; } -static GLboolean gl2psSameColorThreshold(int n, GL2PSrgba rgba[], - GL2PSrgba threshold) { - int i; +static GLboolean gl2psSameColorThreshold(int n, GL2PSrgba rgba[], GL2PSrgba threshold) +{ + int i ; - if (n < 2) - return GL_TRUE; + if (n < 2) return GL_TRUE ; - for (i = 1; i < n; i++) { + for (i = 1 ; i < n; i++) + { if (fabs(rgba[0][0] - rgba[i][0]) > threshold[0] - || fabs(rgba[0][1] - rgba[i][1]) > threshold[1] - || fabs(rgba[0][2] - rgba[i][2]) > threshold[2]) - return GL_FALSE; + || fabs(rgba[0][1] - rgba[i][1]) > threshold[1] + || fabs(rgba[0][2] - rgba[i][2]) > threshold[2]) return GL_FALSE ; } - return GL_TRUE; + return GL_TRUE ; } -static void gl2psSetLastColor(GL2PSrgba rgba) { - int i; - for (i = 0; i < 3; ++i) { - gl2ps->lastrgba[i] = rgba[i]; +static void gl2psSetLastColor(GL2PSrgba rgba) +{ + int i ; + for (i = 0 ; i < 3; ++i) + { + gl2ps->lastrgba[i] = rgba[i] ; } } -static GLfloat gl2psGetRGB(GL2PSimage *im, GLuint x, GLuint y, GLfloat *red, - GLfloat *green, GLfloat *blue) { +static GLfloat gl2psGetRGB(GL2PSimage *im, GLuint x, GLuint y, GLfloat *red, GLfloat *green, + GLfloat *blue) +{ - GLsizei width = im->width; - GLsizei height = im->height; - GLfloat *pixels = im->pixels; - GLfloat *pimag; + GLsizei width = im->width ; + GLsizei height = im->height ; + GLfloat *pixels = im->pixels ; + GLfloat *pimag ; /* OpenGL image is from down to up, PS image is up to down */ - switch (im->format) { - case GL_RGBA: - pimag = pixels + 4 * (width * (height - 1 - y) + x); - break; - case GL_RGB: - default: - pimag = pixels + 3 * (width * (height - 1 - y) + x); - break; - } - *red = *pimag; - pimag++; - *green = *pimag; - pimag++; - *blue = *pimag; - pimag++; - - return (im->format == GL_RGBA) ? *pimag : 1.0F; + switch (im->format) + { + case GL_RGBA : + pimag = pixels + 4 * (width * (height - 1 - y) + x) ; + break ; + case GL_RGB : + default : + pimag = pixels + 3 * (width * (height - 1 - y) + x) ; + break ; + } + *red = *pimag ; + pimag++ ; + *green = *pimag ; + pimag++ ; + *blue = *pimag ; + pimag++ ; + + return (im->format == GL_RGBA) ? *pimag : 1.0F ; } /* Helper routines for pixmaps */ -static GL2PSimage *gl2psCopyPixmap(GL2PSimage *im) { - int size; - GL2PSimage *image = (GL2PSimage*) gl2psMalloc(sizeof(GL2PSimage)); +static GL2PSimage *gl2psCopyPixmap(GL2PSimage *im) +{ + int size ; + GL2PSimage *image = (GL2PSimage*)gl2psMalloc(sizeof(GL2PSimage)) ; - image->width = im->width; - image->height = im->height; - image->format = im->format; - image->type = im->type; + image->width = im->width ; + image->height = im->height ; + image->format = im->format ; + image->type = im->type ; - switch (image->format) { - case GL_RGBA: - size = image->height * image->width * 4 * sizeof(GLfloat); - break; - case GL_RGB: - default: - size = image->height * image->width * 3 * sizeof(GLfloat); - break; + switch (image->format) + { + case GL_RGBA : + size = image->height * image->width * 4 * sizeof(GLfloat) ; + break ; + case GL_RGB : + default : + size = image->height * image->width * 3 * sizeof(GLfloat) ; + break ; } - image->pixels = (GLfloat*) gl2psMalloc(size); - memcpy(image->pixels, im->pixels, size); + image->pixels = (GLfloat*)gl2psMalloc(size) ; + memcpy(image->pixels, im->pixels, size) ; - return image; + return image ; } -static void gl2psFreePixmap(GL2PSimage *im) { - if (!im) - return; - gl2psFree(im->pixels); - gl2psFree(im); +static void gl2psFreePixmap(GL2PSimage *im) +{ + if (!im) return ; + gl2psFree(im->pixels) ; + gl2psFree(im) ; } #if defined(GL2PS_HAVE_LIBPNG) @@ -765,10 +829,10 @@ static void gl2psFreePixmap(GL2PSimage *im) { static void gl2psUserWritePNG(png_structp png_ptr, png_bytep data, png_size_t length) { - unsigned int i; - GL2PSlist *png = (GL2PSlist*)png_get_io_ptr(png_ptr); - for(i = 0; i < length; i++) - gl2psListAdd(png, &data[i]); + unsigned int i ; + GL2PSlist *png = (GL2PSlist*)png_get_io_ptr(png_ptr) ; + for(i = 0 ; i < length ; i++) + gl2psListAdd(png, &data[i]) ; } static void gl2psUserFlushPNG(png_structp png_ptr) @@ -777,162 +841,165 @@ static void gl2psUserFlushPNG(png_structp png_ptr) static void gl2psConvertPixmapToPNG(GL2PSimage *pixmap, GL2PSlist *png) { - png_structp png_ptr; - png_infop info_ptr; - unsigned char *row_data; - GLfloat dr, dg, db; - int row, col; + png_structp png_ptr ; + png_infop info_ptr ; + unsigned char *row_data ; + GLfloat dr, dg, db ; + int row, col ; if(!(png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL))) - return; + return ; - if(!(info_ptr = png_create_info_struct(png_ptr))) { - png_destroy_write_struct(&png_ptr, NULL); - return; + if(!(info_ptr = png_create_info_struct(png_ptr))) + { + png_destroy_write_struct(&png_ptr, NULL) ; + return ; } - if(setjmp(png_jmpbuf(png_ptr))) { - png_destroy_write_struct(&png_ptr, &info_ptr); - return; + if(setjmp(png_jmpbuf(png_ptr))) + { + png_destroy_write_struct(&png_ptr, &info_ptr) ; + return ; } - png_set_write_fn(png_ptr, (void *)png, gl2psUserWritePNG, gl2psUserFlushPNG); - png_set_compression_level(png_ptr, Z_DEFAULT_COMPRESSION); + png_set_write_fn(png_ptr, (void *)png, gl2psUserWritePNG, gl2psUserFlushPNG) ; + png_set_compression_level(png_ptr, Z_DEFAULT_COMPRESSION) ; png_set_IHDR(png_ptr, info_ptr, pixmap->width, pixmap->height, 8, - PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, - PNG_FILTER_TYPE_BASE); - png_write_info(png_ptr, info_ptr); - - row_data = (unsigned char*)gl2psMalloc(3 * pixmap->width * sizeof(unsigned char)); - for(row = 0; row < pixmap->height; row++) { - for(col = 0; col < pixmap->width; col++) { - gl2psGetRGB(pixmap, col, row, &dr, &dg, &db); - row_data[3*col] = (unsigned char)(255. * dr); - row_data[3*col+1] = (unsigned char)(255. * dg); - row_data[3*col+2] = (unsigned char)(255. * db); + PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, + PNG_FILTER_TYPE_BASE) ; + png_write_info(png_ptr, info_ptr) ; + + row_data = (unsigned char*)gl2psMalloc(3 * pixmap->width * sizeof(unsigned char)) ; + for(row = 0 ; row < pixmap->height ; row++) + { + for(col = 0 ; col < pixmap->width ; col++) + { + gl2psGetRGB(pixmap, col, row, &dr, &dg, &db) ; + row_data[3*col] = (unsigned char)(255. * dr) ; + row_data[3*col+1] = (unsigned char)(255. * dg) ; + row_data[3*col+2] = (unsigned char)(255. * db) ; } - png_write_row(png_ptr, (png_bytep)row_data); + png_write_row(png_ptr, (png_bytep)row_data) ; } - gl2psFree(row_data); + gl2psFree(row_data) ; - png_write_end(png_ptr, info_ptr); - png_destroy_write_struct(&png_ptr, &info_ptr); + png_write_end(png_ptr, info_ptr) ; + png_destroy_write_struct(&png_ptr, &info_ptr) ; } #endif /* Helper routines for text strings */ -static GLint gl2psAddText(GLint type, const char *str, const char *fontname, - GLshort fontsize, GLint alignment, GLfloat angle) { - GLfloat pos[4]; - GL2PSprimitive *prim; - GLboolean valid; - - if (!gl2ps || !str || !fontname) - return GL2PS_UNINITIALIZED; - - if (gl2ps->options & GL2PS_NO_TEXT) - return GL2PS_SUCCESS; - - glGetBooleanv(GL_CURRENT_RASTER_POSITION_VALID, &valid); - if (GL_FALSE == valid) - return GL2PS_SUCCESS; /* the primitive is culled */ - - glGetFloatv(GL_CURRENT_RASTER_POSITION, pos); - - prim = (GL2PSprimitive*) gl2psMalloc(sizeof(GL2PSprimitive)); - prim->type = type; - prim->boundary = 0; - prim->numverts = 1; - prim->verts = (GL2PSvertex*) gl2psMalloc(sizeof(GL2PSvertex)); - prim->verts[0].xyz[0] = pos[0]; - prim->verts[0].xyz[1] = pos[1]; - prim->verts[0].xyz[2] = pos[2]; - prim->culled = 0; - prim->offset = 0; - prim->pattern = 0; - prim->factor = 0; - prim->width = 1; - glGetFloatv(GL_CURRENT_RASTER_COLOR, prim->verts[0].rgba); - prim->data.text = (GL2PSstring*) gl2psMalloc(sizeof(GL2PSstring)); - prim->data.text->str = (char*) gl2psMalloc( - (strlen(str) + 1) * sizeof(char)); - strcpy(prim->data.text->str, str); - prim->data.text->fontname = (char*) gl2psMalloc( - (strlen(fontname) + 1) * sizeof(char)); - strcpy(prim->data.text->fontname, fontname); - prim->data.text->fontsize = fontsize; - prim->data.text->alignment = alignment; - prim->data.text->angle = angle; - - gl2psListAdd(gl2ps->auxprimitives, &prim); - glPassThrough(GL2PS_TEXT_TOKEN); - - return GL2PS_SUCCESS; -} - -static GL2PSstring *gl2psCopyText(GL2PSstring *t) { - GL2PSstring *text = (GL2PSstring*) gl2psMalloc(sizeof(GL2PSstring)); - text->str = (char*) gl2psMalloc((strlen(t->str) + 1) * sizeof(char)); - strcpy(text->str, t->str); - text->fontname = (char*) gl2psMalloc( - (strlen(t->fontname) + 1) * sizeof(char)); - strcpy(text->fontname, t->fontname); - text->fontsize = t->fontsize; - text->alignment = t->alignment; - text->angle = t->angle; - - return text; -} - -static void gl2psFreeText(GL2PSstring *text) { - if (!text) - return; - gl2psFree(text->str); - gl2psFree(text->fontname); - gl2psFree(text); +static GLint gl2psAddText(GLint type, const char *str, const char *fontname, GLshort fontsize, + GLint alignment, GLfloat angle) +{ + GLfloat pos[4] ; + GL2PSprimitive *prim ; + GLboolean valid ; + + if (!gl2ps || !str || !fontname) return GL2PS_UNINITIALIZED ; + + if (gl2ps->options & GL2PS_NO_TEXT) return GL2PS_SUCCESS ; + + glGetBooleanv(GL_CURRENT_RASTER_POSITION_VALID, &valid) ; + if (GL_FALSE == valid) return GL2PS_SUCCESS ; /* the primitive is culled */ + + glGetFloatv(GL_CURRENT_RASTER_POSITION, pos) ; + + prim = (GL2PSprimitive*)gl2psMalloc(sizeof(GL2PSprimitive)) ; + prim->type = type ; + prim->boundary = 0 ; + prim->numverts = 1 ; + prim->verts = (GL2PSvertex*)gl2psMalloc(sizeof(GL2PSvertex)) ; + prim->verts[0].xyz[0] = pos[0] ; + prim->verts[0].xyz[1] = pos[1] ; + prim->verts[0].xyz[2] = pos[2] ; + prim->culled = 0 ; + prim->offset = 0 ; + prim->pattern = 0 ; + prim->factor = 0 ; + prim->width = 1 ; + glGetFloatv(GL_CURRENT_RASTER_COLOR, prim->verts[0].rgba) ; + prim->data.text = (GL2PSstring*)gl2psMalloc(sizeof(GL2PSstring)) ; + prim->data.text->str = (char*)gl2psMalloc((strlen(str) + 1) * sizeof(char)) ; + strcpy(prim->data.text->str, str) ; + prim->data.text->fontname = (char*)gl2psMalloc((strlen(fontname) + 1) * sizeof(char)) ; + strcpy(prim->data.text->fontname, fontname) ; + prim->data.text->fontsize = fontsize ; + prim->data.text->alignment = alignment ; + prim->data.text->angle = angle ; + + gl2psListAdd(gl2ps->auxprimitives, &prim) ; + glPassThrough(GL2PS_TEXT_TOKEN) ; + + return GL2PS_SUCCESS ; +} + +static GL2PSstring *gl2psCopyText(GL2PSstring *t) +{ + GL2PSstring *text = (GL2PSstring*)gl2psMalloc(sizeof(GL2PSstring)) ; + text->str = (char*)gl2psMalloc((strlen(t->str) + 1) * sizeof(char)) ; + strcpy(text->str, t->str) ; + text->fontname = (char*)gl2psMalloc((strlen(t->fontname) + 1) * sizeof(char)) ; + strcpy(text->fontname, t->fontname) ; + text->fontsize = t->fontsize ; + text->alignment = t->alignment ; + text->angle = t->angle ; + + return text ; +} + +static void gl2psFreeText(GL2PSstring *text) +{ + if (!text) return ; + gl2psFree(text->str) ; + gl2psFree(text->fontname) ; + gl2psFree(text) ; } /* Helpers for blending modes */ -static GLboolean gl2psSupportedBlendMode(GLenum sfactor, GLenum dfactor) { +static GLboolean gl2psSupportedBlendMode(GLenum sfactor, GLenum dfactor) +{ /* returns TRUE if gl2ps supports the argument combination: only two blending modes have been implemented so far */ if ((sfactor == GL_SRC_ALPHA && dfactor == GL_ONE_MINUS_SRC_ALPHA) - || (sfactor == GL_ONE && dfactor == GL_ZERO)) - return GL_TRUE; - return GL_FALSE; + || (sfactor == GL_ONE && dfactor == GL_ZERO)) return GL_TRUE ; + return GL_FALSE ; } -static void gl2psAdaptVertexForBlending(GL2PSvertex *v) { +static void gl2psAdaptVertexForBlending(GL2PSvertex *v) +{ /* Transforms vertex depending on the actual blending function - currently the vertex v is considered as source vertex and his alpha value is changed to 1.0 if source blending GL_ONE is active. This might be extended in the future */ - if (!v || !gl2ps) - return; + if (!v || !gl2ps) return ; - if (gl2ps->options & GL2PS_NO_BLENDING || !gl2ps->blending) { - v->rgba[3] = 1.0F; - return; + if (gl2ps->options & GL2PS_NO_BLENDING || !gl2ps->blending) + { + v->rgba[3] = 1.0F ; + return ; } - switch (gl2ps->blendfunc[0]) { - case GL_ONE: - v->rgba[3] = 1.0F; - break; - default: - break; + switch (gl2ps->blendfunc[0]) + { + case GL_ONE : + v->rgba[3] = 1.0F ; + break ; + default : + break ; } } -static void gl2psAssignTriangleProperties(GL2PStriangle *t) { +static void gl2psAssignTriangleProperties(GL2PStriangle *t) +{ /* int i; */ - t->prop = T_VAR_COLOR; + t->prop = T_VAR_COLOR ; /* Uncommenting the following lines activates an even more fine grained distinction between triangle types - please don't delete, @@ -950,77 +1017,82 @@ static void gl2psAssignTriangleProperties(GL2PStriangle *t) { */ if (!GL2PS_ZERO(t->vertex[0].rgba[3] - t->vertex[1].rgba[3]) - || !GL2PS_ZERO(t->vertex[1].rgba[3] - t->vertex[2].rgba[3])) { - t->prop |= T_VAR_ALPHA; - } else { + || !GL2PS_ZERO(t->vertex[1].rgba[3] - t->vertex[2].rgba[3])) + { + t->prop |= T_VAR_ALPHA ; + } + else + { if (t->vertex[0].rgba[3] < 1) - t->prop |= T_ALPHA_LESS_1; + t->prop |= T_ALPHA_LESS_1 ; else - t->prop |= T_ALPHA_1; + t->prop |= T_ALPHA_1 ; } } static void gl2psFillTriangleFromPrimitive(GL2PStriangle *t, GL2PSprimitive *p, - GLboolean assignprops) { - t->vertex[0] = p->verts[0]; - t->vertex[1] = p->verts[1]; - t->vertex[2] = p->verts[2]; - if (GL_TRUE == assignprops) - gl2psAssignTriangleProperties(t); + GLboolean assignprops) +{ + t->vertex[0] = p->verts[0] ; + t->vertex[1] = p->verts[1] ; + t->vertex[2] = p->verts[2] ; + if (GL_TRUE == assignprops) gl2psAssignTriangleProperties(t) ; } -static void gl2psInitTriangle(GL2PStriangle *t) { - int i; - GL2PSvertex vertex = { { -1.0F, -1.0F, -1.0F }, - { -1.0F, -1.0F, -1.0F, -1.0F } }; - for (i = 0; i < 3; i++) - t->vertex[i] = vertex; - t->prop = T_UNDEFINED; +static void gl2psInitTriangle(GL2PStriangle *t) +{ + int i ; + GL2PSvertex vertex = { { -1.0F, -1.0F, -1.0F }, { -1.0F, -1.0F, -1.0F, -1.0F } } ; + for (i = 0 ; i < 3; i++) + t->vertex[i] = vertex ; + t->prop = T_UNDEFINED ; } /* Miscellaneous helper routines */ -static GL2PSprimitive *gl2psCopyPrimitive(GL2PSprimitive *p) { - GL2PSprimitive *prim; +static GL2PSprimitive *gl2psCopyPrimitive(GL2PSprimitive *p) +{ + GL2PSprimitive *prim ; - if (!p) { - gl2psMsg(GL2PS_ERROR, "Trying to copy an empty primitive"); - return NULL; + if (!p) + { + gl2psMsg(GL2PS_ERROR, "Trying to copy an empty primitive") ; + return NULL ; } - prim = (GL2PSprimitive*) gl2psMalloc(sizeof(GL2PSprimitive)); + prim = (GL2PSprimitive*)gl2psMalloc(sizeof(GL2PSprimitive)) ; - prim->type = p->type; - prim->numverts = p->numverts; - prim->boundary = p->boundary; - prim->offset = p->offset; - prim->pattern = p->pattern; - prim->factor = p->factor; - prim->culled = p->culled; - prim->width = p->width; - prim->verts = (GL2PSvertex*) gl2psMalloc(p->numverts * sizeof(GL2PSvertex)); - memcpy(prim->verts, p->verts, p->numverts * sizeof(GL2PSvertex)); + prim->type = p->type ; + prim->numverts = p->numverts ; + prim->boundary = p->boundary ; + prim->offset = p->offset ; + prim->pattern = p->pattern ; + prim->factor = p->factor ; + prim->culled = p->culled ; + prim->width = p->width ; + prim->verts = (GL2PSvertex*)gl2psMalloc(p->numverts * sizeof(GL2PSvertex)) ; + memcpy(prim->verts, p->verts, p->numverts * sizeof(GL2PSvertex)) ; - switch (prim->type) { - case GL2PS_PIXMAP: - prim->data.image = gl2psCopyPixmap(p->data.image); - break; - case GL2PS_TEXT: - case GL2PS_SPECIAL: - prim->data.text = gl2psCopyText(p->data.text); - break; - default: - break; + switch (prim->type) + { + case GL2PS_PIXMAP : + prim->data.image = gl2psCopyPixmap(p->data.image) ; + break ; + case GL2PS_TEXT : + case GL2PS_SPECIAL : + prim->data.text = gl2psCopyText(p->data.text) ; + break ; + default : + break ; } - return prim; + return prim ; } -static GLboolean gl2psSamePosition(GL2PSxyz p1, GL2PSxyz p2) { - if (!GL2PS_ZERO(p1[0] - p2[0]) || !GL2PS_ZERO(p1[1] - p2[1]) - || !GL2PS_ZERO(p1[2] - p2[2])) - return GL_FALSE; - return GL_TRUE; +static GLboolean gl2psSamePosition(GL2PSxyz p1, GL2PSxyz p2) +{ + if (!GL2PS_ZERO(p1[0] - p2[0]) || !GL2PS_ZERO(p1[1] - p2[1]) || !GL2PS_ZERO(p1[2] - p2[2])) return GL_FALSE ; + return GL_TRUE ; } /********************************************************************* @@ -1029,648 +1101,731 @@ static GLboolean gl2psSamePosition(GL2PSxyz p1, GL2PSxyz p2) { * *********************************************************************/ -static GLfloat gl2psComparePointPlane(GL2PSxyz point, GL2PSplane plane) { - return (plane[0] * point[0] + plane[1] * point[1] + plane[2] * point[2] - + plane[3]); +static GLfloat gl2psComparePointPlane(GL2PSxyz point, GL2PSplane plane) +{ + return (plane[0] * point[0] + plane[1] * point[1] + plane[2] * point[2] + plane[3]) ; } -static GLfloat gl2psPsca(GLfloat *a, GLfloat *b) { - return (a[0] * b[0] + a[1] * b[1] + a[2] * b[2]); +static GLfloat gl2psPsca(GLfloat *a, GLfloat *b) +{ + return (a[0] * b[0] + a[1] * b[1] + a[2] * b[2]) ; } -static void gl2psPvec(GLfloat *a, GLfloat *b, GLfloat *c) { - c[0] = a[1] * b[2] - a[2] * b[1]; - c[1] = a[2] * b[0] - a[0] * b[2]; - c[2] = a[0] * b[1] - a[1] * b[0]; +static void gl2psPvec(GLfloat *a, GLfloat *b, GLfloat *c) +{ + c[0] = a[1] * b[2] - a[2] * b[1] ; + c[1] = a[2] * b[0] - a[0] * b[2] ; + c[2] = a[0] * b[1] - a[1] * b[0] ; } -static GLfloat gl2psNorm(GLfloat *a) { - return (GLfloat) sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]); +static GLfloat gl2psNorm(GLfloat *a) +{ + return (GLfloat)sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]) ; } -static void gl2psGetNormal(GLfloat *a, GLfloat *b, GLfloat *c) { - GLfloat norm; +static void gl2psGetNormal(GLfloat *a, GLfloat *b, GLfloat *c) +{ + GLfloat norm ; - gl2psPvec(a, b, c); - if (!GL2PS_ZERO(norm = gl2psNorm(c))) { - c[0] = c[0] / norm; - c[1] = c[1] / norm; - c[2] = c[2] / norm; - } else { + gl2psPvec(a, b, c) ; + if (!GL2PS_ZERO(norm = gl2psNorm(c))) + { + c[0] = c[0] / norm ; + c[1] = c[1] / norm ; + c[2] = c[2] / norm ; + } + else + { /* The plane is still wrong despite our tests in gl2psGetPlane. Let's return a dummy value for now (this is a hack: we should do more intelligent tests in GetPlane) */ - c[0] = c[1] = 0.0F; - c[2] = 1.0F; + c[0] = c[1] = 0.0F ; + c[2] = 1.0F ; } } -static void gl2psGetPlane(GL2PSprimitive *prim, GL2PSplane plane) { - GL2PSxyz v = { 0.0F, 0.0F, 0.0F }, w = { 0.0F, 0.0F, 0.0F }; - - switch (prim->type) { - case GL2PS_TRIANGLE: - case GL2PS_QUADRANGLE: - v[0] = prim->verts[1].xyz[0] - prim->verts[0].xyz[0]; - v[1] = prim->verts[1].xyz[1] - prim->verts[0].xyz[1]; - v[2] = prim->verts[1].xyz[2] - prim->verts[0].xyz[2]; - w[0] = prim->verts[2].xyz[0] - prim->verts[0].xyz[0]; - w[1] = prim->verts[2].xyz[1] - prim->verts[0].xyz[1]; - w[2] = prim->verts[2].xyz[2] - prim->verts[0].xyz[2]; +static void gl2psGetPlane(GL2PSprimitive *prim, GL2PSplane plane) +{ + GL2PSxyz v = { 0.0F, 0.0F, 0.0F }, w = { 0.0F, 0.0F, 0.0F } ; + + switch (prim->type) + { + case GL2PS_TRIANGLE : + case GL2PS_QUADRANGLE : + v[0] = prim->verts[1].xyz[0] - prim->verts[0].xyz[0] ; + v[1] = prim->verts[1].xyz[1] - prim->verts[0].xyz[1] ; + v[2] = prim->verts[1].xyz[2] - prim->verts[0].xyz[2] ; + w[0] = prim->verts[2].xyz[0] - prim->verts[0].xyz[0] ; + w[1] = prim->verts[2].xyz[1] - prim->verts[0].xyz[1] ; + w[2] = prim->verts[2].xyz[2] - prim->verts[0].xyz[2] ; if ((GL2PS_ZERO(v[0]) && GL2PS_ZERO(v[1]) && GL2PS_ZERO(v[2])) - || (GL2PS_ZERO(w[0]) && GL2PS_ZERO(w[1]) && GL2PS_ZERO(w[2]))) { - plane[0] = plane[1] = 0.0F; - plane[2] = 1.0F; - plane[3] = -prim->verts[0].xyz[2]; - } else { - gl2psGetNormal(v, w, plane); - plane[3] = -plane[0] * prim->verts[0].xyz[0] - - plane[1] * prim->verts[0].xyz[1] - - plane[2] * prim->verts[0].xyz[2]; + || (GL2PS_ZERO(w[0]) && GL2PS_ZERO(w[1]) && GL2PS_ZERO(w[2]))) + { + plane[0] = plane[1] = 0.0F ; + plane[2] = 1.0F ; + plane[3] = -prim->verts[0].xyz[2] ; } - break; - case GL2PS_LINE: - v[0] = prim->verts[1].xyz[0] - prim->verts[0].xyz[0]; - v[1] = prim->verts[1].xyz[1] - prim->verts[0].xyz[1]; - v[2] = prim->verts[1].xyz[2] - prim->verts[0].xyz[2]; - if (GL2PS_ZERO(v[0]) && GL2PS_ZERO(v[1]) && GL2PS_ZERO(v[2])) { - plane[0] = plane[1] = 0.0F; - plane[2] = 1.0F; - plane[3] = -prim->verts[0].xyz[2]; - } else { + else + { + gl2psGetNormal(v, w, plane) ; + plane[3] = -plane[0] * prim->verts[0].xyz[0] - plane[1] * prim->verts[0].xyz[1] + - plane[2] * prim->verts[0].xyz[2] ; + } + break ; + case GL2PS_LINE : + v[0] = prim->verts[1].xyz[0] - prim->verts[0].xyz[0] ; + v[1] = prim->verts[1].xyz[1] - prim->verts[0].xyz[1] ; + v[2] = prim->verts[1].xyz[2] - prim->verts[0].xyz[2] ; + if (GL2PS_ZERO(v[0]) && GL2PS_ZERO(v[1]) && GL2PS_ZERO(v[2])) + { + plane[0] = plane[1] = 0.0F ; + plane[2] = 1.0F ; + plane[3] = -prim->verts[0].xyz[2] ; + } + else + { if (GL2PS_ZERO(v[0])) - w[0] = 1.0F; + w[0] = 1.0F ; else if (GL2PS_ZERO(v[1])) - w[1] = 1.0F; + w[1] = 1.0F ; else - w[2] = 1.0F; - gl2psGetNormal(v, w, plane); - plane[3] = -plane[0] * prim->verts[0].xyz[0] - - plane[1] * prim->verts[0].xyz[1] - - plane[2] * prim->verts[0].xyz[2]; + w[2] = 1.0F ; + gl2psGetNormal(v, w, plane) ; + plane[3] = -plane[0] * prim->verts[0].xyz[0] - plane[1] * prim->verts[0].xyz[1] + - plane[2] * prim->verts[0].xyz[2] ; } - break; - case GL2PS_POINT: - case GL2PS_PIXMAP: - case GL2PS_TEXT: - case GL2PS_SPECIAL: - case GL2PS_IMAGEMAP: - plane[0] = plane[1] = 0.0F; - plane[2] = 1.0F; - plane[3] = -prim->verts[0].xyz[2]; - break; - default: - gl2psMsg(GL2PS_ERROR, "Unknown primitive type in BSP tree"); - plane[0] = plane[1] = plane[3] = 0.0F; - plane[2] = 1.0F; - break; - } -} - -static void gl2psCutEdge(GL2PSvertex *a, GL2PSvertex *b, GL2PSplane plane, - GL2PSvertex *c) { - GL2PSxyz v; - GLfloat sect, psca; - - v[0] = b->xyz[0] - a->xyz[0]; - v[1] = b->xyz[1] - a->xyz[1]; - v[2] = b->xyz[2] - a->xyz[2]; + break ; + case GL2PS_POINT : + case GL2PS_PIXMAP : + case GL2PS_TEXT : + case GL2PS_SPECIAL : + case GL2PS_IMAGEMAP : + plane[0] = plane[1] = 0.0F ; + plane[2] = 1.0F ; + plane[3] = -prim->verts[0].xyz[2] ; + break ; + default : + gl2psMsg(GL2PS_ERROR, "Unknown primitive type in BSP tree") ; + plane[0] = plane[1] = plane[3] = 0.0F ; + plane[2] = 1.0F ; + break ; + } +} + +static void gl2psCutEdge(GL2PSvertex *a, GL2PSvertex *b, GL2PSplane plane, GL2PSvertex *c) +{ + GL2PSxyz v ; + GLfloat sect, psca ; + + v[0] = b->xyz[0] - a->xyz[0] ; + v[1] = b->xyz[1] - a->xyz[1] ; + v[2] = b->xyz[2] - a->xyz[2] ; if (!GL2PS_ZERO(psca = gl2psPsca(plane, v))) - sect = -gl2psComparePointPlane(a->xyz, plane) / psca; + sect = -gl2psComparePointPlane(a->xyz, plane) / psca ; else - sect = 0.0F; + sect = 0.0F ; - c->xyz[0] = a->xyz[0] + v[0] * sect; - c->xyz[1] = a->xyz[1] + v[1] * sect; - c->xyz[2] = a->xyz[2] + v[2] * sect; + c->xyz[0] = a->xyz[0] + v[0] * sect ; + c->xyz[1] = a->xyz[1] + v[1] * sect ; + c->xyz[2] = a->xyz[2] + v[2] * sect ; - c->rgba[0] = (1 - sect) * a->rgba[0] + sect * b->rgba[0]; - c->rgba[1] = (1 - sect) * a->rgba[1] + sect * b->rgba[1]; - c->rgba[2] = (1 - sect) * a->rgba[2] + sect * b->rgba[2]; - c->rgba[3] = (1 - sect) * a->rgba[3] + sect * b->rgba[3]; + c->rgba[0] = (1 - sect) * a->rgba[0] + sect * b->rgba[0] ; + c->rgba[1] = (1 - sect) * a->rgba[1] + sect * b->rgba[1] ; + c->rgba[2] = (1 - sect) * a->rgba[2] + sect * b->rgba[2] ; + c->rgba[3] = (1 - sect) * a->rgba[3] + sect * b->rgba[3] ; } static void gl2psCreateSplitPrimitive(GL2PSprimitive *parent, GL2PSplane plane, - GL2PSprimitive *child, GLshort numverts, GLshort *index0, - GLshort *index1) { - GLshort i; - - if (parent->type == GL2PS_IMAGEMAP) { - child->type = GL2PS_IMAGEMAP; - child->data.image = parent->data.image; - } else { - if (numverts > 4) { - gl2psMsg(GL2PS_WARNING, "%d vertices in polygon", numverts); - numverts = 4; + GL2PSprimitive *child, GLshort numverts, GLshort *index0, + GLshort *index1) +{ + GLshort i ; + + if (parent->type == GL2PS_IMAGEMAP) + { + child->type = GL2PS_IMAGEMAP ; + child->data.image = parent->data.image ; + } + else + { + if (numverts > 4) + { + gl2psMsg(GL2PS_WARNING, "%d vertices in polygon", numverts) ; + numverts = 4 ; } - switch (numverts) { - case 1: - child->type = GL2PS_POINT; - break; - case 2: - child->type = GL2PS_LINE; - break; - case 3: - child->type = GL2PS_TRIANGLE; - break; - case 4: - child->type = GL2PS_QUADRANGLE; - break; - default: - child->type = GL2PS_NO_TYPE; - break; + switch (numverts) + { + case 1 : + child->type = GL2PS_POINT ; + break ; + case 2 : + child->type = GL2PS_LINE ; + break ; + case 3 : + child->type = GL2PS_TRIANGLE ; + break ; + case 4 : + child->type = GL2PS_QUADRANGLE ; + break ; + default : + child->type = GL2PS_NO_TYPE ; + break ; } } - child->boundary = 0; /* FIXME: not done! */ - child->culled = parent->culled; - child->offset = parent->offset; - child->pattern = parent->pattern; - child->factor = parent->factor; - child->width = parent->width; - child->numverts = numverts; - child->verts = (GL2PSvertex*) gl2psMalloc(numverts * sizeof(GL2PSvertex)); - - for (i = 0; i < numverts; i++) { - if (index1[i] < 0) { - child->verts[i] = parent->verts[index0[i]]; - } else { - gl2psCutEdge(&parent->verts[index0[i]], &parent->verts[index1[i]], - plane, &child->verts[i]); + child->boundary = 0 ; /* FIXME: not done! */ + child->culled = parent->culled ; + child->offset = parent->offset ; + child->pattern = parent->pattern ; + child->factor = parent->factor ; + child->width = parent->width ; + child->numverts = numverts ; + child->verts = (GL2PSvertex*)gl2psMalloc(numverts * sizeof(GL2PSvertex)) ; + + for (i = 0 ; i < numverts; i++) + { + if (index1[i] < 0) + { + child->verts[i] = parent->verts[index0[i]] ; + } + else + { + gl2psCutEdge(&parent->verts[index0[i]], &parent->verts[index1[i]], plane, + &child->verts[i]) ; } } } -static void gl2psAddIndex(GLshort *index0, GLshort *index1, GLshort *nb, - GLshort i, GLshort j) { - GLint k; +static void gl2psAddIndex(GLshort *index0, GLshort *index1, GLshort *nb, GLshort i, GLshort j) +{ + GLint k ; - for (k = 0; k < *nb; k++) { - if ((index0[k] == i && index1[k] == j) - || (index1[k] == i && index0[k] == j)) - return; + for (k = 0 ; k < *nb; k++) + { + if ((index0[k] == i && index1[k] == j) || (index1[k] == i && index0[k] == j)) return ; } - index0[*nb] = i; - index1[*nb] = j; - (*nb)++; + index0[*nb] = i ; + index1[*nb] = j ; + (*nb)++ ; } -static GLshort gl2psGetIndex(GLshort i, GLshort num) { - return (i < num - 1) ? i + 1 : 0; +static GLshort gl2psGetIndex(GLshort i, GLshort num) +{ + return (i < num - 1) ? i + 1 : 0 ; } -static GLint gl2psTestSplitPrimitive(GL2PSprimitive *prim, GL2PSplane plane) { - GLint type = GL2PS_COINCIDENT; - GLshort i, j; - GLfloat d[5]; +static GLint gl2psTestSplitPrimitive(GL2PSprimitive *prim, GL2PSplane plane) +{ + GLint type = GL2PS_COINCIDENT ; + GLshort i, j ; + GLfloat d[5] ; - for (i = 0; i < prim->numverts; i++) { - d[i] = gl2psComparePointPlane(prim->verts[i].xyz, plane); + for (i = 0 ; i < prim->numverts; i++) + { + d[i] = gl2psComparePointPlane(prim->verts[i].xyz, plane) ; } - if (prim->numverts < 2) { - return 0; - } else { - for (i = 0; i < prim->numverts; i++) { - j = gl2psGetIndex(i, prim->numverts); - if (d[j] > GL2PS_EPSILON) { + if (prim->numverts < 2) + { + return 0 ; + } + else + { + for (i = 0 ; i < prim->numverts; i++) + { + j = gl2psGetIndex(i, prim->numverts) ; + if (d[j] > GL2PS_EPSILON) + { if (type == GL2PS_COINCIDENT) - type = GL2PS_IN_BACK_OF; - else if (type != GL2PS_IN_BACK_OF) - return 1; - if (d[i] < -GL2PS_EPSILON) - return 1; - } else if (d[j] < -GL2PS_EPSILON) { + type = GL2PS_IN_BACK_OF ; + else if (type != GL2PS_IN_BACK_OF) return 1 ; + if (d[i] < -GL2PS_EPSILON) return 1 ; + } + else if (d[j] < -GL2PS_EPSILON) + { if (type == GL2PS_COINCIDENT) - type = GL2PS_IN_FRONT_OF; - else if (type != GL2PS_IN_FRONT_OF) - return 1; - if (d[i] > GL2PS_EPSILON) - return 1; + type = GL2PS_IN_FRONT_OF ; + else if (type != GL2PS_IN_FRONT_OF) return 1 ; + if (d[i] > GL2PS_EPSILON) return 1 ; } } } - return 0; + return 0 ; } -static GLint gl2psSplitPrimitive(GL2PSprimitive *prim, GL2PSplane plane, - GL2PSprimitive **front, GL2PSprimitive **back) { - GLshort i, j, in = 0, out = 0, in0[5], in1[5], out0[5], out1[5]; - GLint type; - GLfloat d[5]; +static GLint gl2psSplitPrimitive(GL2PSprimitive *prim, GL2PSplane plane, GL2PSprimitive **front, + GL2PSprimitive **back) +{ + GLshort i, j, in = 0, out = 0, in0[5], in1[5], out0[5], out1[5] ; + GLint type ; + GLfloat d[5] ; - type = GL2PS_COINCIDENT; + type = GL2PS_COINCIDENT ; - for (i = 0; i < prim->numverts; i++) { - d[i] = gl2psComparePointPlane(prim->verts[i].xyz, plane); + for (i = 0 ; i < prim->numverts; i++) + { + d[i] = gl2psComparePointPlane(prim->verts[i].xyz, plane) ; } - switch (prim->type) { - case GL2PS_POINT: + switch (prim->type) + { + case GL2PS_POINT : if (d[0] > GL2PS_EPSILON) - type = GL2PS_IN_BACK_OF; + type = GL2PS_IN_BACK_OF ; else if (d[0] < -GL2PS_EPSILON) - type = GL2PS_IN_FRONT_OF; + type = GL2PS_IN_FRONT_OF ; else - type = GL2PS_COINCIDENT; - break; - default: - for (i = 0; i < prim->numverts; i++) { - j = gl2psGetIndex(i, prim->numverts); - if (d[j] > GL2PS_EPSILON) { + type = GL2PS_COINCIDENT ; + break ; + default : + for (i = 0 ; i < prim->numverts; i++) + { + j = gl2psGetIndex(i, prim->numverts) ; + if (d[j] > GL2PS_EPSILON) + { if (type == GL2PS_COINCIDENT) - type = GL2PS_IN_BACK_OF; - else if (type != GL2PS_IN_BACK_OF) - type = GL2PS_SPANNING; - if (d[i] < -GL2PS_EPSILON) { - gl2psAddIndex(in0, in1, &in, i, j); - gl2psAddIndex(out0, out1, &out, i, j); - type = GL2PS_SPANNING; + type = GL2PS_IN_BACK_OF ; + else if (type != GL2PS_IN_BACK_OF) type = GL2PS_SPANNING ; + if (d[i] < -GL2PS_EPSILON) + { + gl2psAddIndex(in0, in1, &in, i, j) ; + gl2psAddIndex(out0, out1, &out, i, j) ; + type = GL2PS_SPANNING ; } - gl2psAddIndex(out0, out1, &out, j, -1); - } else if (d[j] < -GL2PS_EPSILON) { + gl2psAddIndex(out0, out1, &out, j, -1) ; + } + else if (d[j] < -GL2PS_EPSILON) + { if (type == GL2PS_COINCIDENT) - type = GL2PS_IN_FRONT_OF; - else if (type != GL2PS_IN_FRONT_OF) - type = GL2PS_SPANNING; - if (d[i] > GL2PS_EPSILON) { - gl2psAddIndex(in0, in1, &in, i, j); - gl2psAddIndex(out0, out1, &out, i, j); - type = GL2PS_SPANNING; + type = GL2PS_IN_FRONT_OF ; + else if (type != GL2PS_IN_FRONT_OF) type = GL2PS_SPANNING ; + if (d[i] > GL2PS_EPSILON) + { + gl2psAddIndex(in0, in1, &in, i, j) ; + gl2psAddIndex(out0, out1, &out, i, j) ; + type = GL2PS_SPANNING ; } - gl2psAddIndex(in0, in1, &in, j, -1); - } else { - gl2psAddIndex(in0, in1, &in, j, -1); - gl2psAddIndex(out0, out1, &out, j, -1); + gl2psAddIndex(in0, in1, &in, j, -1) ; + } + else + { + gl2psAddIndex(in0, in1, &in, j, -1) ; + gl2psAddIndex(out0, out1, &out, j, -1) ; } } - break; + break ; } - if (type == GL2PS_SPANNING) { - *back = (GL2PSprimitive*) gl2psMalloc(sizeof(GL2PSprimitive)); - *front = (GL2PSprimitive*) gl2psMalloc(sizeof(GL2PSprimitive)); - gl2psCreateSplitPrimitive(prim, plane, *back, out, out0, out1); - gl2psCreateSplitPrimitive(prim, plane, *front, in, in0, in1); + if (type == GL2PS_SPANNING) + { + *back = (GL2PSprimitive*)gl2psMalloc(sizeof(GL2PSprimitive)) ; + *front = (GL2PSprimitive*)gl2psMalloc(sizeof(GL2PSprimitive)) ; + gl2psCreateSplitPrimitive(prim, plane, *back, out, out0, out1) ; + gl2psCreateSplitPrimitive(prim, plane, *front, in, in0, in1) ; } - return type; -} - -static void gl2psDivideQuad(GL2PSprimitive *quad, GL2PSprimitive **t1, - GL2PSprimitive **t2) { - *t1 = (GL2PSprimitive*) gl2psMalloc(sizeof(GL2PSprimitive)); - *t2 = (GL2PSprimitive*) gl2psMalloc(sizeof(GL2PSprimitive)); - (*t1)->type = (*t2)->type = GL2PS_TRIANGLE; - (*t1)->numverts = (*t2)->numverts = 3; - (*t1)->culled = (*t2)->culled = quad->culled; - (*t1)->offset = (*t2)->offset = quad->offset; - (*t1)->pattern = (*t2)->pattern = quad->pattern; - (*t1)->factor = (*t2)->factor = quad->factor; - (*t1)->width = (*t2)->width = quad->width; - (*t1)->verts = (GL2PSvertex*) gl2psMalloc(3 * sizeof(GL2PSvertex)); - (*t2)->verts = (GL2PSvertex*) gl2psMalloc(3 * sizeof(GL2PSvertex)); - (*t1)->verts[0] = quad->verts[0]; - (*t1)->verts[1] = quad->verts[1]; - (*t1)->verts[2] = quad->verts[2]; - (*t1)->boundary = ((quad->boundary & 1) ? 1 : 0) - | ((quad->boundary & 2) ? 2 : 0); - (*t2)->verts[0] = quad->verts[0]; - (*t2)->verts[1] = quad->verts[2]; - (*t2)->verts[2] = quad->verts[3]; - (*t2)->boundary = ((quad->boundary & 4) ? 2 : 0) - | ((quad->boundary & 4) ? 2 : 0); + return type ; } -static int gl2psCompareDepth(const void *a, const void *b) { - GL2PSprimitive *q, *w; - GLfloat dq = 0.0F, dw = 0.0F, diff; - int i; +static void gl2psDivideQuad(GL2PSprimitive *quad, GL2PSprimitive **t1, GL2PSprimitive **t2) +{ + *t1 = (GL2PSprimitive*)gl2psMalloc(sizeof(GL2PSprimitive)) ; + *t2 = (GL2PSprimitive*)gl2psMalloc(sizeof(GL2PSprimitive)) ; + (*t1)->type = (*t2)->type = GL2PS_TRIANGLE ; + (*t1)->numverts = (*t2)->numverts = 3 ; + (*t1)->culled = (*t2)->culled = quad->culled ; + (*t1)->offset = (*t2)->offset = quad->offset ; + (*t1)->pattern = (*t2)->pattern = quad->pattern ; + (*t1)->factor = (*t2)->factor = quad->factor ; + (*t1)->width = (*t2)->width = quad->width ; + (*t1)->verts = (GL2PSvertex*)gl2psMalloc(3 * sizeof(GL2PSvertex)) ; + (*t2)->verts = (GL2PSvertex*)gl2psMalloc(3 * sizeof(GL2PSvertex)) ; + (*t1)->verts[0] = quad->verts[0] ; + (*t1)->verts[1] = quad->verts[1] ; + (*t1)->verts[2] = quad->verts[2] ; + (*t1)->boundary = ((quad->boundary & 1) ? 1 : 0) | ((quad->boundary & 2) ? 2 : 0) ; + (*t2)->verts[0] = quad->verts[0] ; + (*t2)->verts[1] = quad->verts[2] ; + (*t2)->verts[2] = quad->verts[3] ; + (*t2)->boundary = ((quad->boundary & 4) ? 2 : 0) | ((quad->boundary & 4) ? 2 : 0) ; +} + +static int gl2psCompareDepth(const void *a, const void *b) +{ + GL2PSprimitive *q, *w ; + GLfloat dq = 0.0F, dw = 0.0F, diff ; + int i ; - q = *(GL2PSprimitive**) a; - w = *(GL2PSprimitive**) b; + q = *(GL2PSprimitive**)a ; + w = *(GL2PSprimitive**)b ; - for (i = 0; i < q->numverts; i++) { - dq += q->verts[i].xyz[2]; + for (i = 0 ; i < q->numverts; i++) + { + dq += q->verts[i].xyz[2] ; } - dq /= (GLfloat) q->numverts; + dq /= (GLfloat)q->numverts ; - for (i = 0; i < w->numverts; i++) { - dw += w->verts[i].xyz[2]; + for (i = 0 ; i < w->numverts; i++) + { + dw += w->verts[i].xyz[2] ; } - dw /= (GLfloat) w->numverts; + dw /= (GLfloat)w->numverts ; - diff = dq - dw; - if (diff > 0.) { - return -1; - } else if (diff < 0.) { - return 1; - } else { - return 0; + diff = dq - dw ; + if (diff > 0.) + { + return -1 ; + } + else if (diff < 0.) + { + return 1 ; + } + else + { + return 0 ; } } -static int gl2psTrianglesFirst(const void *a, const void *b) { - GL2PSprimitive *q, *w; +static int gl2psTrianglesFirst(const void *a, const void *b) +{ + GL2PSprimitive *q, *w ; - q = *(GL2PSprimitive**) a; - w = *(GL2PSprimitive**) b; - return (q->type < w->type ? 1 : -1); + q = *(GL2PSprimitive**)a ; + w = *(GL2PSprimitive**)b ; + return (q->type < w->type ? 1 : -1) ; } -static GLint gl2psFindRoot(GL2PSlist *primitives, GL2PSprimitive **root) { - GLint i, j, count, best = 1000000, index = 0; - GL2PSprimitive *prim1, *prim2; - GL2PSplane plane; - GLint maxp; +static GLint gl2psFindRoot(GL2PSlist *primitives, GL2PSprimitive **root) +{ + GLint i, j, count, best = 1000000, index = 0 ; + GL2PSprimitive *prim1, *prim2 ; + GL2PSplane plane ; + GLint maxp ; - if (!gl2psListNbr(primitives)) { - gl2psMsg(GL2PS_ERROR, "Cannot fint root in empty primitive list"); - return 0; + if (!gl2psListNbr(primitives)) + { + gl2psMsg(GL2PS_ERROR, "Cannot fint root in empty primitive list") ; + return 0 ; } - *root = *(GL2PSprimitive**) gl2psListPointer(primitives, 0); + *root = *(GL2PSprimitive**)gl2psListPointer(primitives, 0) ; - if (gl2ps->options & GL2PS_BEST_ROOT) { - maxp = gl2psListNbr(primitives); - if (maxp > gl2ps->maxbestroot) { - maxp = gl2ps->maxbestroot; + if (gl2ps->options & GL2PS_BEST_ROOT) + { + maxp = gl2psListNbr(primitives) ; + if (maxp > gl2ps->maxbestroot) + { + maxp = gl2ps->maxbestroot ; } - for (i = 0; i < maxp; i++) { - prim1 = *(GL2PSprimitive**) gl2psListPointer(primitives, i); - gl2psGetPlane(prim1, plane); - count = 0; - for (j = 0; j < gl2psListNbr(primitives); j++) { - if (j != i) { - prim2 = *(GL2PSprimitive**) gl2psListPointer(primitives, j); - count += gl2psTestSplitPrimitive(prim2, plane); + for (i = 0 ; i < maxp; i++) + { + prim1 = *(GL2PSprimitive**)gl2psListPointer(primitives, i) ; + gl2psGetPlane(prim1, plane) ; + count = 0 ; + for (j = 0 ; j < gl2psListNbr(primitives); j++) + { + if (j != i) + { + prim2 = *(GL2PSprimitive**)gl2psListPointer(primitives, j) ; + count += gl2psTestSplitPrimitive(prim2, plane) ; } - if (count > best) - break; + if (count > best) break ; } - if (count < best) { - best = count; - index = i; - *root = prim1; - if (!count) - return index; + if (count < best) + { + best = count ; + index = i ; + *root = prim1 ; + if (!count) return index ; } } /* if(index) gl2psMsg(GL2PS_INFO, "GL2PS_BEST_ROOT was worth it: %d", index); */ - return index; - } else { - return 0; + return index ; + } + else + { + return 0 ; } } -static void gl2psFreeImagemap(GL2PSimagemap *list) { - GL2PSimagemap *next; - while (list != NULL) { - next = list->next; - gl2psFree(list->image->pixels); - gl2psFree(list->image); - gl2psFree(list); - list = next; +static void gl2psFreeImagemap(GL2PSimagemap *list) +{ + GL2PSimagemap *next ; + while (list != NULL) + { + next = list->next ; + gl2psFree(list->image->pixels) ; + gl2psFree(list->image) ; + gl2psFree(list) ; + list = next ; } } -static void gl2psFreePrimitive(void *data) { - GL2PSprimitive *q; +static void gl2psFreePrimitive(void *data) +{ + GL2PSprimitive *q ; - q = *(GL2PSprimitive**) data; - gl2psFree(q->verts); - if (q->type == GL2PS_TEXT || q->type == GL2PS_SPECIAL) { - gl2psFreeText(q->data.text); - } else if (q->type == GL2PS_PIXMAP) { - gl2psFreePixmap(q->data.image); + q = *(GL2PSprimitive**)data ; + gl2psFree(q->verts) ; + if (q->type == GL2PS_TEXT || q->type == GL2PS_SPECIAL) + { + gl2psFreeText(q->data.text) ; + } + else if (q->type == GL2PS_PIXMAP) + { + gl2psFreePixmap(q->data.image) ; } - gl2psFree(q); + gl2psFree(q) ; } -static void gl2psAddPrimitiveInList(GL2PSprimitive *prim, GL2PSlist *list) { - GL2PSprimitive *t1, *t2; +static void gl2psAddPrimitiveInList(GL2PSprimitive *prim, GL2PSlist *list) +{ + GL2PSprimitive *t1, *t2 ; - if (prim->type != GL2PS_QUADRANGLE) { - gl2psListAdd(list, &prim); - } else { - gl2psDivideQuad(prim, &t1, &t2); - gl2psListAdd(list, &t1); - gl2psListAdd(list, &t2); - gl2psFreePrimitive(&prim); + if (prim->type != GL2PS_QUADRANGLE) + { + gl2psListAdd(list, &prim) ; + } + else + { + gl2psDivideQuad(prim, &t1, &t2) ; + gl2psListAdd(list, &t1) ; + gl2psListAdd(list, &t2) ; + gl2psFreePrimitive(&prim) ; } } -static void gl2psFreeBspTree(GL2PSbsptree **tree) { - if (*tree) { - if ((*tree)->back) - gl2psFreeBspTree(&(*tree)->back); - if ((*tree)->primitives) { - gl2psListAction((*tree)->primitives, gl2psFreePrimitive); - gl2psListDelete((*tree)->primitives); +static void gl2psFreeBspTree(GL2PSbsptree **tree) +{ + if (*tree) + { + if ((*tree)->back) gl2psFreeBspTree(&(*tree)->back) ; + if ((*tree)->primitives) + { + gl2psListAction((*tree)->primitives, gl2psFreePrimitive) ; + gl2psListDelete((*tree)->primitives) ; } - if ((*tree)->front) - gl2psFreeBspTree(&(*tree)->front); - gl2psFree(*tree); - *tree = NULL; + if ((*tree)->front) gl2psFreeBspTree(&(*tree)->front) ; + gl2psFree(*tree) ; + *tree = NULL ; } } -static GLboolean gl2psGreater(GLfloat f1, GLfloat f2) { +static GLboolean gl2psGreater(GLfloat f1, GLfloat f2) +{ if (f1 > f2) - return GL_TRUE; + return GL_TRUE ; else - return GL_FALSE; + return GL_FALSE ; } -static GLboolean gl2psLess(GLfloat f1, GLfloat f2) { +static GLboolean gl2psLess(GLfloat f1, GLfloat f2) +{ if (f1 < f2) - return GL_TRUE; + return GL_TRUE ; else - return GL_FALSE; -} - -static void gl2psBuildBspTree(GL2PSbsptree *tree, GL2PSlist *primitives) { - GL2PSprimitive *prim, *frontprim = NULL, *backprim = NULL; - GL2PSlist *frontlist, *backlist; - GLint i, index; - - tree->front = NULL; - tree->back = NULL; - tree->primitives = gl2psListCreate(1, 2, sizeof(GL2PSprimitive*)); - index = gl2psFindRoot(primitives, &prim); - gl2psGetPlane(prim, tree->plane); - gl2psAddPrimitiveInList(prim, tree->primitives); - - frontlist = gl2psListCreate(1, 2, sizeof(GL2PSprimitive*)); - backlist = gl2psListCreate(1, 2, sizeof(GL2PSprimitive*)); - - for (i = 0; i < gl2psListNbr(primitives); i++) { - if (i != index) { - prim = *(GL2PSprimitive**) gl2psListPointer(primitives, i); - switch (gl2psSplitPrimitive(prim, tree->plane, &frontprim, - &backprim)) { - case GL2PS_COINCIDENT: - gl2psAddPrimitiveInList(prim, tree->primitives); - break; - case GL2PS_IN_BACK_OF: - gl2psAddPrimitiveInList(prim, backlist); - break; - case GL2PS_IN_FRONT_OF: - gl2psAddPrimitiveInList(prim, frontlist); - break; - case GL2PS_SPANNING: - gl2psAddPrimitiveInList(backprim, backlist); - gl2psAddPrimitiveInList(frontprim, frontlist); - gl2psFreePrimitive(&prim); - break; + return GL_FALSE ; +} + +static void gl2psBuildBspTree(GL2PSbsptree *tree, GL2PSlist *primitives) +{ + GL2PSprimitive *prim, *frontprim = NULL, *backprim = NULL ; + GL2PSlist *frontlist, *backlist ; + GLint i, index ; + + tree->front = NULL ; + tree->back = NULL ; + tree->primitives = gl2psListCreate(1, 2, sizeof(GL2PSprimitive*)) ; + index = gl2psFindRoot(primitives, &prim) ; + gl2psGetPlane(prim, tree->plane) ; + gl2psAddPrimitiveInList(prim, tree->primitives) ; + + frontlist = gl2psListCreate(1, 2, sizeof(GL2PSprimitive*)) ; + backlist = gl2psListCreate(1, 2, sizeof(GL2PSprimitive*)) ; + + for (i = 0 ; i < gl2psListNbr(primitives); i++) + { + if (i != index) + { + prim = *(GL2PSprimitive**)gl2psListPointer(primitives, i) ; + switch (gl2psSplitPrimitive(prim, tree->plane, &frontprim, &backprim)) + { + case GL2PS_COINCIDENT : + gl2psAddPrimitiveInList(prim, tree->primitives) ; + break ; + case GL2PS_IN_BACK_OF : + gl2psAddPrimitiveInList(prim, backlist) ; + break ; + case GL2PS_IN_FRONT_OF : + gl2psAddPrimitiveInList(prim, frontlist) ; + break ; + case GL2PS_SPANNING : + gl2psAddPrimitiveInList(backprim, backlist) ; + gl2psAddPrimitiveInList(frontprim, frontlist) ; + gl2psFreePrimitive(&prim) ; + break ; } } } - if (gl2psListNbr(tree->primitives)) { - gl2psListSort(tree->primitives, gl2psTrianglesFirst); + if (gl2psListNbr(tree->primitives)) + { + gl2psListSort(tree->primitives, gl2psTrianglesFirst) ; } - if (gl2psListNbr(frontlist)) { - gl2psListSort(frontlist, gl2psTrianglesFirst); - tree->front = (GL2PSbsptree*) gl2psMalloc(sizeof(GL2PSbsptree)); - gl2psBuildBspTree(tree->front, frontlist); - } else { - gl2psListDelete(frontlist); + if (gl2psListNbr(frontlist)) + { + gl2psListSort(frontlist, gl2psTrianglesFirst) ; + tree->front = (GL2PSbsptree*)gl2psMalloc(sizeof(GL2PSbsptree)) ; + gl2psBuildBspTree(tree->front, frontlist) ; + } + else + { + gl2psListDelete(frontlist) ; } - if (gl2psListNbr(backlist)) { - gl2psListSort(backlist, gl2psTrianglesFirst); - tree->back = (GL2PSbsptree*) gl2psMalloc(sizeof(GL2PSbsptree)); - gl2psBuildBspTree(tree->back, backlist); - } else { - gl2psListDelete(backlist); + if (gl2psListNbr(backlist)) + { + gl2psListSort(backlist, gl2psTrianglesFirst) ; + tree->back = (GL2PSbsptree*)gl2psMalloc(sizeof(GL2PSbsptree)) ; + gl2psBuildBspTree(tree->back, backlist) ; + } + else + { + gl2psListDelete(backlist) ; } - gl2psListDelete(primitives); + gl2psListDelete(primitives) ; } -static void gl2psTraverseBspTree(GL2PSbsptree *tree, GL2PSxyz eye, - GLfloat epsilon, GLboolean(*compare)(GLfloat f1, GLfloat f2), - void(*action)(void *data), int inverse) { - GLfloat result; +static void gl2psTraverseBspTree(GL2PSbsptree *tree, GL2PSxyz eye, GLfloat epsilon, + GLboolean(*compare)(GLfloat f1, GLfloat f2), + void(*action)(void *data), int inverse) +{ + GLfloat result ; - if (!tree) - return; + if (!tree) return ; - result = gl2psComparePointPlane(eye, tree->plane); + result = gl2psComparePointPlane(eye, tree->plane) ; - if (GL_TRUE == compare(result, epsilon)) { - gl2psTraverseBspTree(tree->back, eye, epsilon, compare, action, - inverse); - if (inverse) { - gl2psListActionInverse(tree->primitives, action); - } else { - gl2psListAction(tree->primitives, action); + if (GL_TRUE == compare(result, epsilon)) + { + gl2psTraverseBspTree(tree->back, eye, epsilon, compare, action, inverse) ; + if (inverse) + { + gl2psListActionInverse(tree->primitives, action) ; + } + else + { + gl2psListAction(tree->primitives, action) ; + } + gl2psTraverseBspTree(tree->front, eye, epsilon, compare, action, inverse) ; + } + else if (GL_TRUE == compare(-epsilon, result)) + { + gl2psTraverseBspTree(tree->front, eye, epsilon, compare, action, inverse) ; + if (inverse) + { + gl2psListActionInverse(tree->primitives, action) ; } - gl2psTraverseBspTree(tree->front, eye, epsilon, compare, action, - inverse); - } else if (GL_TRUE == compare(-epsilon, result)) { - gl2psTraverseBspTree(tree->front, eye, epsilon, compare, action, - inverse); - if (inverse) { - gl2psListActionInverse(tree->primitives, action); - } else { - gl2psListAction(tree->primitives, action); + else + { + gl2psListAction(tree->primitives, action) ; } - gl2psTraverseBspTree(tree->back, eye, epsilon, compare, action, - inverse); - } else { - gl2psTraverseBspTree(tree->front, eye, epsilon, compare, action, - inverse); - gl2psTraverseBspTree(tree->back, eye, epsilon, compare, action, - inverse); + gl2psTraverseBspTree(tree->back, eye, epsilon, compare, action, inverse) ; + } + else + { + gl2psTraverseBspTree(tree->front, eye, epsilon, compare, action, inverse) ; + gl2psTraverseBspTree(tree->back, eye, epsilon, compare, action, inverse) ; } } -static void gl2psRescaleAndOffset() { - GL2PSprimitive *prim; - GLfloat minZ, maxZ, rangeZ, scaleZ; - GLfloat factor, units, area, dZ, dZdX, dZdY, maxdZ; - int i, j; +static void gl2psRescaleAndOffset() +{ + GL2PSprimitive *prim ; + GLfloat minZ, maxZ, rangeZ, scaleZ ; + GLfloat factor, units, area, dZ, dZdX, dZdY, maxdZ ; + int i, j ; - if (!gl2psListNbr(gl2ps->primitives)) - return; + if (!gl2psListNbr(gl2ps->primitives)) return ; /* get z-buffer range */ - prim = *(GL2PSprimitive**) gl2psListPointer(gl2ps->primitives, 0); - minZ = maxZ = prim->verts[0].xyz[2]; - for (i = 1; i < prim->numverts; i++) { - if (prim->verts[i].xyz[2] < minZ) - minZ = prim->verts[i].xyz[2]; - if (prim->verts[i].xyz[2] > maxZ) - maxZ = prim->verts[i].xyz[2]; - } - for (i = 1; i < gl2psListNbr(gl2ps->primitives); i++) { - prim = *(GL2PSprimitive**) gl2psListPointer(gl2ps->primitives, i); - for (j = 0; j < prim->numverts; j++) { - if (prim->verts[j].xyz[2] < minZ) - minZ = prim->verts[j].xyz[2]; - if (prim->verts[j].xyz[2] > maxZ) - maxZ = prim->verts[j].xyz[2]; + prim = *(GL2PSprimitive**)gl2psListPointer(gl2ps->primitives, 0) ; + minZ = maxZ = prim->verts[0].xyz[2] ; + for (i = 1 ; i < prim->numverts; i++) + { + if (prim->verts[i].xyz[2] < minZ) minZ = prim->verts[i].xyz[2] ; + if (prim->verts[i].xyz[2] > maxZ) maxZ = prim->verts[i].xyz[2] ; + } + for (i = 1 ; i < gl2psListNbr(gl2ps->primitives); i++) + { + prim = *(GL2PSprimitive**)gl2psListPointer(gl2ps->primitives, i) ; + for (j = 0 ; j < prim->numverts; j++) + { + if (prim->verts[j].xyz[2] < minZ) minZ = prim->verts[j].xyz[2] ; + if (prim->verts[j].xyz[2] > maxZ) maxZ = prim->verts[j].xyz[2] ; } } - rangeZ = (maxZ - minZ); + rangeZ = (maxZ - minZ) ; /* rescale z-buffer coordinate in [0,GL2PS_ZSCALE], to make it of the same order of magnitude as the x and y coordinates */ - scaleZ = GL2PS_ZERO(rangeZ) ? GL2PS_ZSCALE : (GL2PS_ZSCALE / rangeZ); + scaleZ = GL2PS_ZERO(rangeZ) ? GL2PS_ZSCALE : (GL2PS_ZSCALE / rangeZ) ; /* avoid precision loss (we use floats!) */ - if (scaleZ > 100000.F) - scaleZ = 100000.F; + if (scaleZ > 100000.F) scaleZ = 100000.F ; /* apply offsets */ - for (i = 0; i < gl2psListNbr(gl2ps->primitives); i++) { - prim = *(GL2PSprimitive**) gl2psListPointer(gl2ps->primitives, i); - for (j = 0; j < prim->numverts; j++) { - prim->verts[j].xyz[2] = (prim->verts[j].xyz[2] - minZ) * scaleZ; + for (i = 0 ; i < gl2psListNbr(gl2ps->primitives); i++) + { + prim = *(GL2PSprimitive**)gl2psListPointer(gl2ps->primitives, i) ; + for (j = 0 ; j < prim->numverts; j++) + { + prim->verts[j].xyz[2] = (prim->verts[j].xyz[2] - minZ) * scaleZ ; } - if ((gl2ps->options & GL2PS_SIMPLE_LINE_OFFSET) - && (prim->type == GL2PS_LINE)) { - if (gl2ps->sort == GL2PS_SIMPLE_SORT) { - prim->verts[0].xyz[2] -= GL2PS_ZOFFSET_LARGE; - prim->verts[1].xyz[2] -= GL2PS_ZOFFSET_LARGE; - } else { - prim->verts[0].xyz[2] -= GL2PS_ZOFFSET; - prim->verts[1].xyz[2] -= GL2PS_ZOFFSET; + if ((gl2ps->options & GL2PS_SIMPLE_LINE_OFFSET) && (prim->type == GL2PS_LINE)) + { + if (gl2ps->sort == GL2PS_SIMPLE_SORT) + { + prim->verts[0].xyz[2] -= GL2PS_ZOFFSET_LARGE ; + prim->verts[1].xyz[2] -= GL2PS_ZOFFSET_LARGE ; } - } else if (prim->offset && (prim->type == GL2PS_TRIANGLE)) { - factor = gl2ps->offset[0]; - units = gl2ps->offset[1]; + else + { + prim->verts[0].xyz[2] -= GL2PS_ZOFFSET ; + prim->verts[1].xyz[2] -= GL2PS_ZOFFSET ; + } + } + else if (prim->offset && (prim->type == GL2PS_TRIANGLE)) + { + factor = gl2ps->offset[0] ; + units = gl2ps->offset[1] ; area = (prim->verts[1].xyz[0] - prim->verts[0].xyz[0]) - * (prim->verts[2].xyz[1] - prim->verts[1].xyz[1]) - - (prim->verts[2].xyz[0] - prim->verts[1].xyz[0]) - * (prim->verts[1].xyz[1] - prim->verts[0].xyz[1]); - if (!GL2PS_ZERO(area)) { - dZdX = - ((prim->verts[2].xyz[1] - prim->verts[1].xyz[1]) - * (prim->verts[1].xyz[2] - prim->verts[0].xyz[2]) - - (prim->verts[1].xyz[1] - prim->verts[0].xyz[1]) - * (prim->verts[2].xyz[2] - - prim->verts[1].xyz[2])) - / area; - dZdY = - ((prim->verts[1].xyz[0] - prim->verts[0].xyz[0]) - * (prim->verts[2].xyz[2] - prim->verts[1].xyz[2]) - - (prim->verts[2].xyz[0] - prim->verts[1].xyz[0]) - * (prim->verts[1].xyz[2] - - prim->verts[0].xyz[2])) - / area; - maxdZ = (GLfloat) sqrt(dZdX * dZdX + dZdY * dZdY); - } else { - maxdZ = 0.0F; + * (prim->verts[2].xyz[1] - prim->verts[1].xyz[1]) + - (prim->verts[2].xyz[0] - prim->verts[1].xyz[0]) + * (prim->verts[1].xyz[1] - prim->verts[0].xyz[1]) ; + if (!GL2PS_ZERO(area)) + { + dZdX = ((prim->verts[2].xyz[1] - prim->verts[1].xyz[1]) + * (prim->verts[1].xyz[2] - prim->verts[0].xyz[2]) + - (prim->verts[1].xyz[1] - prim->verts[0].xyz[1]) + * (prim->verts[2].xyz[2] - prim->verts[1].xyz[2])) / area ; + dZdY = ((prim->verts[1].xyz[0] - prim->verts[0].xyz[0]) + * (prim->verts[2].xyz[2] - prim->verts[1].xyz[2]) + - (prim->verts[2].xyz[0] - prim->verts[1].xyz[0]) + * (prim->verts[1].xyz[2] - prim->verts[0].xyz[2])) / area ; + maxdZ = (GLfloat)sqrt(dZdX * dZdX + dZdY * dZdY) ; } - dZ = factor * maxdZ + units; - prim->verts[0].xyz[2] += dZ; - prim->verts[1].xyz[2] += dZ; - prim->verts[2].xyz[2] += dZ; + else + { + maxdZ = 0.0F ; + } + dZ = factor * maxdZ + units ; + prim->verts[0].xyz[2] += dZ ; + prim->verts[1].xyz[2] += dZ ; + prim->verts[2].xyz[2] += dZ ; } } } @@ -1681,446 +1836,499 @@ static void gl2psRescaleAndOffset() { * *********************************************************************/ -static GLint gl2psGetPlaneFromPoints(GL2PSxyz a, GL2PSxyz b, GL2PSplane plane) { - GLfloat n; - - plane[0] = b[1] - a[1]; - plane[1] = a[0] - b[0]; - n = (GLfloat) sqrt(plane[0] * plane[0] + plane[1] * plane[1]); - plane[2] = 0.0F; - if (!GL2PS_ZERO(n)) { - plane[0] /= n; - plane[1] /= n; - plane[3] = -plane[0] * a[0] - plane[1] * a[1]; - return 1; - } else { - plane[0] = -1.0F; - plane[1] = 0.0F; - plane[3] = a[0]; - return 0; +static GLint gl2psGetPlaneFromPoints(GL2PSxyz a, GL2PSxyz b, GL2PSplane plane) +{ + GLfloat n ; + + plane[0] = b[1] - a[1] ; + plane[1] = a[0] - b[0] ; + n = (GLfloat)sqrt(plane[0] * plane[0] + plane[1] * plane[1]) ; + plane[2] = 0.0F ; + if (!GL2PS_ZERO(n)) + { + plane[0] /= n ; + plane[1] /= n ; + plane[3] = -plane[0] * a[0] - plane[1] * a[1] ; + return 1 ; + } + else + { + plane[0] = -1.0F ; + plane[1] = 0.0F ; + plane[3] = a[0] ; + return 0 ; } } -static void gl2psFreeBspImageTree(GL2PSbsptree2d **tree) { - if (*tree) { - if ((*tree)->back) - gl2psFreeBspImageTree(&(*tree)->back); - if ((*tree)->front) - gl2psFreeBspImageTree(&(*tree)->front); - gl2psFree(*tree); - *tree = NULL; +static void gl2psFreeBspImageTree(GL2PSbsptree2d **tree) +{ + if (*tree) + { + if ((*tree)->back) gl2psFreeBspImageTree(&(*tree)->back) ; + if ((*tree)->front) gl2psFreeBspImageTree(&(*tree)->front) ; + gl2psFree(*tree) ; + *tree = NULL ; } } -static GLint gl2psCheckPoint(GL2PSxyz point, GL2PSplane plane) { - GLfloat pt_dis; +static GLint gl2psCheckPoint(GL2PSxyz point, GL2PSplane plane) +{ + GLfloat pt_dis ; - pt_dis = gl2psComparePointPlane(point, plane); + pt_dis = gl2psComparePointPlane(point, plane) ; if (pt_dis > GL2PS_EPSILON) - return GL2PS_POINT_INFRONT; + return GL2PS_POINT_INFRONT ; else if (pt_dis < -GL2PS_EPSILON) - return GL2PS_POINT_BACK; + return GL2PS_POINT_BACK ; else - return GL2PS_POINT_COINCIDENT; + return GL2PS_POINT_COINCIDENT ; } -static void gl2psAddPlanesInBspTreeImage(GL2PSprimitive *prim, - GL2PSbsptree2d **tree) { - GLint ret = 0; - GLint i; - GLint offset = 0; - GL2PSbsptree2d *head = NULL, *cur = NULL; +static void gl2psAddPlanesInBspTreeImage(GL2PSprimitive *prim, GL2PSbsptree2d **tree) +{ + GLint ret = 0 ; + GLint i ; + GLint offset = 0 ; + GL2PSbsptree2d *head = NULL, *cur = NULL ; - if ((*tree == NULL) && (prim->numverts > 2)) { + if ((*tree == NULL) && (prim->numverts > 2)) + { /* don't cull if transparent for(i = 0; i < prim->numverts - 1; i++) if(prim->verts[i].rgba[3] < 1.0F) return; */ - head = (GL2PSbsptree2d*) gl2psMalloc(sizeof(GL2PSbsptree2d)); - for (i = 0; i < prim->numverts - 1; i++) { - if (!gl2psGetPlaneFromPoints(prim->verts[i].xyz, - prim->verts[i + 1].xyz, head->plane)) { - if (prim->numverts - i > 3) { - offset++; - } else { - gl2psFree(head); - return; + head = (GL2PSbsptree2d*)gl2psMalloc(sizeof(GL2PSbsptree2d)) ; + for (i = 0 ; i < prim->numverts - 1; i++) + { + if (!gl2psGetPlaneFromPoints(prim->verts[i].xyz, prim->verts[i + 1].xyz, head->plane)) + { + if (prim->numverts - i > 3) + { + offset++ ; + } + else + { + gl2psFree(head) ; + return ; } - } else { - break; + } + else + { + break ; } } - head->back = NULL; - head->front = NULL; - for (i = 2 + offset; i < prim->numverts; i++) { - ret = gl2psCheckPoint(prim->verts[i].xyz, head->plane); - if (ret != GL2PS_POINT_COINCIDENT) - break; + head->back = NULL ; + head->front = NULL ; + for (i = 2 + offset ; i < prim->numverts; i++) + { + ret = gl2psCheckPoint(prim->verts[i].xyz, head->plane) ; + if (ret != GL2PS_POINT_COINCIDENT) break ; } - switch (ret) { - case GL2PS_POINT_INFRONT: - cur = head; - for (i = 1 + offset; i < prim->numverts - 1; i++) { - if (cur->front == NULL) { - cur->front = (GL2PSbsptree2d*) gl2psMalloc( - sizeof(GL2PSbsptree2d)); + switch (ret) + { + case GL2PS_POINT_INFRONT : + cur = head ; + for (i = 1 + offset ; i < prim->numverts - 1; i++) + { + if (cur->front == NULL) + { + cur->front = (GL2PSbsptree2d*)gl2psMalloc(sizeof(GL2PSbsptree2d)) ; } - if (gl2psGetPlaneFromPoints(prim->verts[i].xyz, - prim->verts[i + 1].xyz, cur->front->plane)) { - cur = cur->front; - cur->front = NULL; - cur->back = NULL; + if (gl2psGetPlaneFromPoints(prim->verts[i].xyz, prim->verts[i + 1].xyz, + cur->front->plane)) + { + cur = cur->front ; + cur->front = NULL ; + cur->back = NULL ; } } - if (cur->front == NULL) { - cur->front = (GL2PSbsptree2d*) gl2psMalloc( - sizeof(GL2PSbsptree2d)); + if (cur->front == NULL) + { + cur->front = (GL2PSbsptree2d*)gl2psMalloc(sizeof(GL2PSbsptree2d)) ; + } + if (gl2psGetPlaneFromPoints(prim->verts[i].xyz, prim->verts[offset].xyz, + cur->front->plane)) + { + cur->front->front = NULL ; + cur->front->back = NULL ; } - if (gl2psGetPlaneFromPoints(prim->verts[i].xyz, - prim->verts[offset].xyz, cur->front->plane)) { - cur->front->front = NULL; - cur->front->back = NULL; - } else { - gl2psFree(cur->front); - cur->front = NULL; + else + { + gl2psFree(cur->front) ; + cur->front = NULL ; } - break; - case GL2PS_POINT_BACK: - for (i = 0; i < 4; i++) { - head->plane[i] = -head->plane[i]; + break ; + case GL2PS_POINT_BACK : + for (i = 0 ; i < 4; i++) + { + head->plane[i] = -head->plane[i] ; } - cur = head; - for (i = 1 + offset; i < prim->numverts - 1; i++) { - if (cur->front == NULL) { - cur->front = (GL2PSbsptree2d*) gl2psMalloc( - sizeof(GL2PSbsptree2d)); + cur = head ; + for (i = 1 + offset ; i < prim->numverts - 1; i++) + { + if (cur->front == NULL) + { + cur->front = (GL2PSbsptree2d*)gl2psMalloc(sizeof(GL2PSbsptree2d)) ; } - if (gl2psGetPlaneFromPoints(prim->verts[i + 1].xyz, - prim->verts[i].xyz, cur->front->plane)) { - cur = cur->front; - cur->front = NULL; - cur->back = NULL; + if (gl2psGetPlaneFromPoints(prim->verts[i + 1].xyz, prim->verts[i].xyz, + cur->front->plane)) + { + cur = cur->front ; + cur->front = NULL ; + cur->back = NULL ; } } - if (cur->front == NULL) { - cur->front = (GL2PSbsptree2d*) gl2psMalloc( - sizeof(GL2PSbsptree2d)); + if (cur->front == NULL) + { + cur->front = (GL2PSbsptree2d*)gl2psMalloc(sizeof(GL2PSbsptree2d)) ; } - if (gl2psGetPlaneFromPoints(prim->verts[offset].xyz, - prim->verts[i].xyz, cur->front->plane)) { - cur->front->front = NULL; - cur->front->back = NULL; - } else { - gl2psFree(cur->front); - cur->front = NULL; + if (gl2psGetPlaneFromPoints(prim->verts[offset].xyz, prim->verts[i].xyz, + cur->front->plane)) + { + cur->front->front = NULL ; + cur->front->back = NULL ; } - break; - default: - gl2psFree(head); - return; + else + { + gl2psFree(cur->front) ; + cur->front = NULL ; + } + break ; + default : + gl2psFree(head) ; + return ; } - (*tree) = head; + (*tree) = head ; } } -static GLint gl2psCheckPrimitive(GL2PSprimitive *prim, GL2PSplane plane) { - GLint i; - GLint pos; +static GLint gl2psCheckPrimitive(GL2PSprimitive *prim, GL2PSplane plane) +{ + GLint i ; + GLint pos ; - pos = gl2psCheckPoint(prim->verts[0].xyz, plane); - for (i = 1; i < prim->numverts; i++) { - pos |= gl2psCheckPoint(prim->verts[i].xyz, plane); - if (pos == (GL2PS_POINT_INFRONT | GL2PS_POINT_BACK)) - return GL2PS_SPANNING; + pos = gl2psCheckPoint(prim->verts[0].xyz, plane) ; + for (i = 1 ; i < prim->numverts; i++) + { + pos |= gl2psCheckPoint(prim->verts[i].xyz, plane) ; + if (pos == (GL2PS_POINT_INFRONT | GL2PS_POINT_BACK)) return GL2PS_SPANNING ; } if (pos & GL2PS_POINT_INFRONT) - return GL2PS_IN_FRONT_OF; + return GL2PS_IN_FRONT_OF ; else if (pos & GL2PS_POINT_BACK) - return GL2PS_IN_BACK_OF; + return GL2PS_IN_BACK_OF ; else - return GL2PS_COINCIDENT; -} - -static GL2PSprimitive *gl2psCreateSplitPrimitive2D(GL2PSprimitive *parent, - GLshort numverts, GL2PSvertex *vertx) { - GLint i; - GL2PSprimitive *child = (GL2PSprimitive*) gl2psMalloc( - sizeof(GL2PSprimitive)); - - if (parent->type == GL2PS_IMAGEMAP) { - child->type = GL2PS_IMAGEMAP; - child->data.image = parent->data.image; - } else { - switch (numverts) { - case 1: - child->type = GL2PS_POINT; - break; - case 2: - child->type = GL2PS_LINE; - break; - case 3: - child->type = GL2PS_TRIANGLE; - break; - case 4: - child->type = GL2PS_QUADRANGLE; - break; - default: - child->type = GL2PS_NO_TYPE; - break; /* FIXME */ + return GL2PS_COINCIDENT ; +} + +static GL2PSprimitive *gl2psCreateSplitPrimitive2D(GL2PSprimitive *parent, GLshort numverts, + GL2PSvertex *vertx) +{ + GLint i ; + GL2PSprimitive *child = (GL2PSprimitive*)gl2psMalloc(sizeof(GL2PSprimitive)) ; + + if (parent->type == GL2PS_IMAGEMAP) + { + child->type = GL2PS_IMAGEMAP ; + child->data.image = parent->data.image ; + } + else + { + switch (numverts) + { + case 1 : + child->type = GL2PS_POINT ; + break ; + case 2 : + child->type = GL2PS_LINE ; + break ; + case 3 : + child->type = GL2PS_TRIANGLE ; + break ; + case 4 : + child->type = GL2PS_QUADRANGLE ; + break ; + default : + child->type = GL2PS_NO_TYPE ; + break ; /* FIXME */ } } - child->boundary = 0; /* FIXME: not done! */ - child->culled = parent->culled; - child->offset = parent->offset; - child->pattern = parent->pattern; - child->factor = parent->factor; - child->width = parent->width; - child->numverts = numverts; - child->verts = (GL2PSvertex*) gl2psMalloc(numverts * sizeof(GL2PSvertex)); - for (i = 0; i < numverts; i++) { - child->verts[i] = vertx[i]; + child->boundary = 0 ; /* FIXME: not done! */ + child->culled = parent->culled ; + child->offset = parent->offset ; + child->pattern = parent->pattern ; + child->factor = parent->factor ; + child->width = parent->width ; + child->numverts = numverts ; + child->verts = (GL2PSvertex*)gl2psMalloc(numverts * sizeof(GL2PSvertex)) ; + for (i = 0 ; i < numverts; i++) + { + child->verts[i] = vertx[i] ; } - return child; + return child ; } -static void gl2psSplitPrimitive2D(GL2PSprimitive *prim, GL2PSplane plane, - GL2PSprimitive **front, GL2PSprimitive **back) { +static void gl2psSplitPrimitive2D(GL2PSprimitive *prim, GL2PSplane plane, GL2PSprimitive **front, + GL2PSprimitive **back) +{ /* cur will hold the position of the current vertex prev will hold the position of the previous vertex prev0 will hold the position of the vertex number 0 v1 and v2 represent the current and previous vertices, respectively flag is set if the current vertex should be checked against the plane */ - GLint cur = -1, prev = -1, i, v1 = 0, v2 = 0, flag = 1, prev0 = -1; + GLint cur = -1, prev = -1, i, v1 = 0, v2 = 0, flag = 1, prev0 = -1 ; /* list of vertices that will go in front and back primitive */ - GL2PSvertex *front_list = NULL, *back_list = NULL; + GL2PSvertex *front_list = NULL, *back_list = NULL ; /* number of vertices in front and back list */ - GLshort front_count = 0, back_count = 0; - - for (i = 0; i <= prim->numverts; i++) { - v1 = i; - if (v1 == prim->numverts) { - if (prim->numverts < 3) - break; - v1 = 0; - v2 = prim->numverts - 1; - cur = prev0; - } else if (flag) { - cur = gl2psCheckPoint(prim->verts[v1].xyz, plane); - if (i == 0) { - prev0 = cur; + GLshort front_count = 0, back_count = 0 ; + + for (i = 0 ; i <= prim->numverts; i++) + { + v1 = i ; + if (v1 == prim->numverts) + { + if (prim->numverts < 3) break ; + v1 = 0 ; + v2 = prim->numverts - 1 ; + cur = prev0 ; + } + else if (flag) + { + cur = gl2psCheckPoint(prim->verts[v1].xyz, plane) ; + if (i == 0) + { + prev0 = cur ; } } - if (((prev == -1) || (prev == cur) || (prev == 0) || (cur == 0)) - && (i < prim->numverts)) { - if (cur == GL2PS_POINT_INFRONT) { - front_count++; - front_list = (GL2PSvertex*) gl2psRealloc(front_list, - sizeof(GL2PSvertex) * front_count); - front_list[front_count - 1] = prim->verts[v1]; - } else if (cur == GL2PS_POINT_BACK) { - back_count++; - back_list = (GL2PSvertex*) gl2psRealloc(back_list, - sizeof(GL2PSvertex) * back_count); - back_list[back_count - 1] = prim->verts[v1]; - } else { - front_count++; - front_list = (GL2PSvertex*) gl2psRealloc(front_list, - sizeof(GL2PSvertex) * front_count); - front_list[front_count - 1] = prim->verts[v1]; - back_count++; - back_list = (GL2PSvertex*) gl2psRealloc(back_list, - sizeof(GL2PSvertex) * back_count); - back_list[back_count - 1] = prim->verts[v1]; + if (((prev == -1) || (prev == cur) || (prev == 0) || (cur == 0)) && (i < prim->numverts)) + { + if (cur == GL2PS_POINT_INFRONT) + { + front_count++ ; + front_list = (GL2PSvertex*)gl2psRealloc(front_list, + sizeof(GL2PSvertex) * front_count) ; + front_list[front_count - 1] = prim->verts[v1] ; + } + else if (cur == GL2PS_POINT_BACK) + { + back_count++ ; + back_list = (GL2PSvertex*)gl2psRealloc(back_list, + sizeof(GL2PSvertex) * back_count) ; + back_list[back_count - 1] = prim->verts[v1] ; } - flag = 1; - } else if ((prev != cur) && (cur != 0) && (prev != 0)) { - if (v1 != 0) { - v2 = v1 - 1; - i--; + else + { + front_count++ ; + front_list = (GL2PSvertex*)gl2psRealloc(front_list, + sizeof(GL2PSvertex) * front_count) ; + front_list[front_count - 1] = prim->verts[v1] ; + back_count++ ; + back_list = (GL2PSvertex*)gl2psRealloc(back_list, + sizeof(GL2PSvertex) * back_count) ; + back_list[back_count - 1] = prim->verts[v1] ; + } + flag = 1 ; + } + else if ((prev != cur) && (cur != 0) && (prev != 0)) + { + if (v1 != 0) + { + v2 = v1 - 1 ; + i-- ; } - front_count++; - front_list = (GL2PSvertex*) gl2psRealloc(front_list, - sizeof(GL2PSvertex) * front_count); - gl2psCutEdge(&prim->verts[v2], &prim->verts[v1], plane, - &front_list[front_count - 1]); - back_count++; - back_list = (GL2PSvertex*) gl2psRealloc(back_list, - sizeof(GL2PSvertex) * back_count); - back_list[back_count - 1] = front_list[front_count - 1]; - flag = 0; + front_count++ ; + front_list = (GL2PSvertex*)gl2psRealloc(front_list, sizeof(GL2PSvertex) * front_count) ; + gl2psCutEdge(&prim->verts[v2], &prim->verts[v1], plane, &front_list[front_count - 1]) ; + back_count++ ; + back_list = (GL2PSvertex*)gl2psRealloc(back_list, sizeof(GL2PSvertex) * back_count) ; + back_list[back_count - 1] = front_list[front_count - 1] ; + flag = 0 ; } - prev = cur; + prev = cur ; } - *front = gl2psCreateSplitPrimitive2D(prim, front_count, front_list); - *back = gl2psCreateSplitPrimitive2D(prim, back_count, back_list); - gl2psFree(front_list); - gl2psFree(back_list); + *front = gl2psCreateSplitPrimitive2D(prim, front_count, front_list) ; + *back = gl2psCreateSplitPrimitive2D(prim, back_count, back_list) ; + gl2psFree(front_list) ; + gl2psFree(back_list) ; } -static GLint gl2psAddInBspImageTree(GL2PSprimitive *prim, GL2PSbsptree2d **tree) { - GLint ret = 0; - GL2PSprimitive *frontprim = NULL, *backprim = NULL; +static GLint gl2psAddInBspImageTree(GL2PSprimitive *prim, GL2PSbsptree2d **tree) +{ + GLint ret = 0 ; + GL2PSprimitive *frontprim = NULL, *backprim = NULL ; /* FIXME: until we consider the actual extent of text strings and pixmaps, never cull them. Otherwise the whole string/pixmap gets culled as soon as the reference point is hidden */ - if (prim->type == GL2PS_PIXMAP || prim->type == GL2PS_TEXT - || prim->type == GL2PS_SPECIAL) { - return 1; + if (prim->type == GL2PS_PIXMAP || prim->type == GL2PS_TEXT || prim->type == GL2PS_SPECIAL) + { + return 1 ; } - if (*tree == NULL) { - if ((prim->type != GL2PS_IMAGEMAP) - && (GL_FALSE == gl2ps->zerosurfacearea)) { - gl2psAddPlanesInBspTreeImage(gl2ps->primitivetoadd, tree); + if (*tree == NULL) + { + if ((prim->type != GL2PS_IMAGEMAP) && (GL_FALSE == gl2ps->zerosurfacearea)) + { + gl2psAddPlanesInBspTreeImage(gl2ps->primitivetoadd, tree) ; } - return 1; - } else { - switch (gl2psCheckPrimitive(prim, (*tree)->plane)) { - case GL2PS_IN_BACK_OF: - return gl2psAddInBspImageTree(prim, &(*tree)->back); - case GL2PS_IN_FRONT_OF: + return 1 ; + } + else + { + switch (gl2psCheckPrimitive(prim, (*tree)->plane)) + { + case GL2PS_IN_BACK_OF : + return gl2psAddInBspImageTree(prim, &(*tree)->back) ; + case GL2PS_IN_FRONT_OF : if ((*tree)->front != NULL) - return gl2psAddInBspImageTree(prim, &(*tree)->front); + return gl2psAddInBspImageTree(prim, &(*tree)->front) ; else - return 0; - case GL2PS_SPANNING: - gl2psSplitPrimitive2D(prim, (*tree)->plane, &frontprim, &backprim); - ret = gl2psAddInBspImageTree(backprim, &(*tree)->back); - if ((*tree)->front != NULL) { - if (gl2psAddInBspImageTree(frontprim, &(*tree)->front)) { - ret = 1; + return 0 ; + case GL2PS_SPANNING : + gl2psSplitPrimitive2D(prim, (*tree)->plane, &frontprim, &backprim) ; + ret = gl2psAddInBspImageTree(backprim, &(*tree)->back) ; + if ((*tree)->front != NULL) + { + if (gl2psAddInBspImageTree(frontprim, &(*tree)->front)) + { + ret = 1 ; } } - gl2psFree(frontprim->verts); - gl2psFree(frontprim); - gl2psFree(backprim->verts); - gl2psFree(backprim); - return ret; - case GL2PS_COINCIDENT: - if ((*tree)->back != NULL) { - gl2ps->zerosurfacearea = GL_TRUE; - ret = gl2psAddInBspImageTree(prim, &(*tree)->back); - gl2ps->zerosurfacearea = GL_FALSE; - if (ret) - return ret; + gl2psFree(frontprim->verts) ; + gl2psFree(frontprim) ; + gl2psFree(backprim->verts) ; + gl2psFree(backprim) ; + return ret ; + case GL2PS_COINCIDENT : + if ((*tree)->back != NULL) + { + gl2ps->zerosurfacearea = GL_TRUE ; + ret = gl2psAddInBspImageTree(prim, &(*tree)->back) ; + gl2ps->zerosurfacearea = GL_FALSE ; + if (ret) return ret ; } - if ((*tree)->front != NULL) { - gl2ps->zerosurfacearea = GL_TRUE; - ret = gl2psAddInBspImageTree(prim, &(*tree)->front); - gl2ps->zerosurfacearea = GL_FALSE; - if (ret) - return ret; + if ((*tree)->front != NULL) + { + gl2ps->zerosurfacearea = GL_TRUE ; + ret = gl2psAddInBspImageTree(prim, &(*tree)->front) ; + gl2ps->zerosurfacearea = GL_FALSE ; + if (ret) return ret ; } if (prim->type == GL2PS_LINE) - return 1; + return 1 ; else - return 0; + return 0 ; } } - return 0; + return 0 ; } -static void gl2psAddInImageTree(void *data) { - GL2PSprimitive *prim = *(GL2PSprimitive **) data; - gl2ps->primitivetoadd = prim; - if (prim->type == GL2PS_IMAGEMAP - && prim->data.image->format == GL2PS_IMAGEMAP_VISIBLE) { - prim->culled = 1; - } else if (!gl2psAddInBspImageTree(prim, &gl2ps->imagetree)) { - prim->culled = 1; - } else if (prim->type == GL2PS_IMAGEMAP) { - prim->data.image->format = GL2PS_IMAGEMAP_VISIBLE; +static void gl2psAddInImageTree(void *data) +{ + GL2PSprimitive *prim = *(GL2PSprimitive **)data ; + gl2ps->primitivetoadd = prim ; + if (prim->type == GL2PS_IMAGEMAP && prim->data.image->format == GL2PS_IMAGEMAP_VISIBLE) + { + prim->culled = 1 ; + } + else if (!gl2psAddInBspImageTree(prim, &gl2ps->imagetree)) + { + prim->culled = 1 ; + } + else if (prim->type == GL2PS_IMAGEMAP) + { + prim->data.image->format = GL2PS_IMAGEMAP_VISIBLE ; } } /* Boundary construction */ -static void gl2psAddBoundaryInList(GL2PSprimitive *prim, GL2PSlist *list) { - GL2PSprimitive *b; - GLshort i; - GL2PSxyz c; - - c[0] = c[1] = c[2] = 0.0F; - for (i = 0; i < prim->numverts; i++) { - c[0] += prim->verts[i].xyz[0]; - c[1] += prim->verts[i].xyz[1]; - } - c[0] /= prim->numverts; - c[1] /= prim->numverts; - - for (i = 0; i < prim->numverts; i++) { - if (prim->boundary & (GLint) pow(2., i)) { - b = (GL2PSprimitive*) gl2psMalloc(sizeof(GL2PSprimitive)); - b->type = GL2PS_LINE; - b->offset = prim->offset; - b->pattern = prim->pattern; - b->factor = prim->factor; - b->culled = prim->culled; - b->width = prim->width; - b->boundary = 0; - b->numverts = 2; - b->verts = (GL2PSvertex*) gl2psMalloc(2 * sizeof(GL2PSvertex)); +static void gl2psAddBoundaryInList(GL2PSprimitive *prim, GL2PSlist *list) +{ + GL2PSprimitive *b ; + GLshort i ; + GL2PSxyz c ; + + c[0] = c[1] = c[2] = 0.0F ; + for (i = 0 ; i < prim->numverts; i++) + { + c[0] += prim->verts[i].xyz[0] ; + c[1] += prim->verts[i].xyz[1] ; + } + c[0] /= prim->numverts ; + c[1] /= prim->numverts ; + + for (i = 0 ; i < prim->numverts; i++) + { + if (prim->boundary & (GLint)pow(2., i)) + { + b = (GL2PSprimitive*)gl2psMalloc(sizeof(GL2PSprimitive)) ; + b->type = GL2PS_LINE ; + b->offset = prim->offset ; + b->pattern = prim->pattern ; + b->factor = prim->factor ; + b->culled = prim->culled ; + b->width = prim->width ; + b->boundary = 0 ; + b->numverts = 2 ; + b->verts = (GL2PSvertex*)gl2psMalloc(2 * sizeof(GL2PSvertex)) ; #if 0 /* FIXME: need to work on boundary offset... */ - v[0] = c[0] - prim->verts[i].xyz[0]; - v[1] = c[1] - prim->verts[i].xyz[1]; - v[2] = 0.0F; - norm = gl2psNorm(v); - v[0] /= norm; - v[1] /= norm; - b->verts[0].xyz[0] = prim->verts[i].xyz[0] +0.1*v[0]; - b->verts[0].xyz[1] = prim->verts[i].xyz[1] +0.1*v[1]; - b->verts[0].xyz[2] = prim->verts[i].xyz[2]; - v[0] = c[0] - prim->verts[gl2psGetIndex(i, prim->numverts)].xyz[0]; - v[1] = c[1] - prim->verts[gl2psGetIndex(i, prim->numverts)].xyz[1]; - norm = gl2psNorm(v); - v[0] /= norm; - v[1] /= norm; - b->verts[1].xyz[0] = prim->verts[gl2psGetIndex(i, prim->numverts)].xyz[0] +0.1*v[0]; - b->verts[1].xyz[1] = prim->verts[gl2psGetIndex(i, prim->numverts)].xyz[1] +0.1*v[1]; - b->verts[1].xyz[2] = prim->verts[gl2psGetIndex(i, prim->numverts)].xyz[2]; + v[0] = c[0] - prim->verts[i].xyz[0] ; + v[1] = c[1] - prim->verts[i].xyz[1] ; + v[2] = 0.0F ; + norm = gl2psNorm(v) ; + v[0] /= norm ; + v[1] /= norm ; + b->verts[0].xyz[0] = prim->verts[i].xyz[0] +0.1*v[0] ; + b->verts[0].xyz[1] = prim->verts[i].xyz[1] +0.1*v[1] ; + b->verts[0].xyz[2] = prim->verts[i].xyz[2] ; + v[0] = c[0] - prim->verts[gl2psGetIndex(i, prim->numverts)].xyz[0] ; + v[1] = c[1] - prim->verts[gl2psGetIndex(i, prim->numverts)].xyz[1] ; + norm = gl2psNorm(v) ; + v[0] /= norm ; + v[1] /= norm ; + b->verts[1].xyz[0] = prim->verts[gl2psGetIndex(i, prim->numverts)].xyz[0] +0.1*v[0] ; + b->verts[1].xyz[1] = prim->verts[gl2psGetIndex(i, prim->numverts)].xyz[1] +0.1*v[1] ; + b->verts[1].xyz[2] = prim->verts[gl2psGetIndex(i, prim->numverts)].xyz[2] ; #else - b->verts[0].xyz[0] = prim->verts[i].xyz[0]; - b->verts[0].xyz[1] = prim->verts[i].xyz[1]; - b->verts[0].xyz[2] = prim->verts[i].xyz[2]; - b->verts[1].xyz[0] = - prim->verts[gl2psGetIndex(i, prim->numverts)].xyz[0]; - b->verts[1].xyz[1] = - prim->verts[gl2psGetIndex(i, prim->numverts)].xyz[1]; - b->verts[1].xyz[2] = - prim->verts[gl2psGetIndex(i, prim->numverts)].xyz[2]; + b->verts[0].xyz[0] = prim->verts[i].xyz[0] ; + b->verts[0].xyz[1] = prim->verts[i].xyz[1] ; + b->verts[0].xyz[2] = prim->verts[i].xyz[2] ; + b->verts[1].xyz[0] = prim->verts[gl2psGetIndex(i, prim->numverts)].xyz[0] ; + b->verts[1].xyz[1] = prim->verts[gl2psGetIndex(i, prim->numverts)].xyz[1] ; + b->verts[1].xyz[2] = prim->verts[gl2psGetIndex(i, prim->numverts)].xyz[2] ; #endif - b->verts[0].rgba[0] = 0.0F; - b->verts[0].rgba[1] = 0.0F; - b->verts[0].rgba[2] = 0.0F; - b->verts[0].rgba[3] = 0.0F; - b->verts[1].rgba[0] = 0.0F; - b->verts[1].rgba[1] = 0.0F; - b->verts[1].rgba[2] = 0.0F; - b->verts[1].rgba[3] = 0.0F; - gl2psListAdd(list, &b); + b->verts[0].rgba[0] = 0.0F ; + b->verts[0].rgba[1] = 0.0F ; + b->verts[0].rgba[2] = 0.0F ; + b->verts[0].rgba[3] = 0.0F ; + b->verts[1].rgba[0] = 0.0F ; + b->verts[1].rgba[1] = 0.0F ; + b->verts[1].rgba[2] = 0.0F ; + b->verts[1].rgba[3] = 0.0F ; + gl2psListAdd(list, &b) ; } } } -static void gl2psBuildPolygonBoundary(GL2PSbsptree *tree) { - GLint i; - GL2PSprimitive *prim; +static void gl2psBuildPolygonBoundary(GL2PSbsptree *tree) +{ + GLint i ; + GL2PSprimitive *prim ; - if (!tree) - return; - gl2psBuildPolygonBoundary(tree->back); - for (i = 0; i < gl2psListNbr(tree->primitives); i++) { - prim = *(GL2PSprimitive**) gl2psListPointer(tree->primitives, i); - if (prim->boundary) - gl2psAddBoundaryInList(prim, tree->primitives); + if (!tree) return ; + gl2psBuildPolygonBoundary(tree->back) ; + for (i = 0 ; i < gl2psListNbr(tree->primitives); i++) + { + prim = *(GL2PSprimitive**)gl2psListPointer(tree->primitives, i) ; + if (prim->boundary) gl2psAddBoundaryInList(prim, tree->primitives) ; } - gl2psBuildPolygonBoundary(tree->front); + gl2psBuildPolygonBoundary(tree->front) ; } /********************************************************************* @@ -2129,283 +2337,290 @@ static void gl2psBuildPolygonBoundary(GL2PSbsptree *tree) { * *********************************************************************/ -static void gl2psAddPolyPrimitive(GLshort type, GLshort numverts, - GL2PSvertex *verts, GLint offset, GLushort pattern, GLint factor, - GLfloat width, char boundary) { - GL2PSprimitive *prim; - - prim = (GL2PSprimitive*) gl2psMalloc(sizeof(GL2PSprimitive)); - prim->type = type; - prim->numverts = numverts; - prim->verts = (GL2PSvertex*) gl2psMalloc(numverts * sizeof(GL2PSvertex)); - memcpy(prim->verts, verts, numverts * sizeof(GL2PSvertex)); - prim->boundary = boundary; - prim->offset = offset; - prim->pattern = pattern; - prim->factor = factor; - prim->width = width; - prim->culled = 0; +static void gl2psAddPolyPrimitive(GLshort type, GLshort numverts, GL2PSvertex *verts, GLint offset, + GLushort pattern, GLint factor, GLfloat width, char boundary) +{ + GL2PSprimitive *prim ; + + prim = (GL2PSprimitive*)gl2psMalloc(sizeof(GL2PSprimitive)) ; + prim->type = type ; + prim->numverts = numverts ; + prim->verts = (GL2PSvertex*)gl2psMalloc(numverts * sizeof(GL2PSvertex)) ; + memcpy(prim->verts, verts, numverts * sizeof(GL2PSvertex)) ; + prim->boundary = boundary ; + prim->offset = offset ; + prim->pattern = pattern ; + prim->factor = factor ; + prim->width = width ; + prim->culled = 0 ; /* FIXME: here we should have an option to split stretched tris/quads to enhance SIMPLE_SORT */ - gl2psListAdd(gl2ps->primitives, &prim); -} - -static GLint gl2psGetVertex(GL2PSvertex *v, GLfloat *p) { - GLint i; - - v->xyz[0] = p[0]; - v->xyz[1] = p[1]; - v->xyz[2] = p[2]; - - if (gl2ps->colormode == GL_COLOR_INDEX && gl2ps->colorsize > 0) { - i = (GLint)(p[3] + 0.5); - v->rgba[0] = gl2ps->colormap[i][0]; - v->rgba[1] = gl2ps->colormap[i][1]; - v->rgba[2] = gl2ps->colormap[i][2]; - v->rgba[3] = gl2ps->colormap[i][3]; - return 4; - } else { - v->rgba[0] = p[3]; - v->rgba[1] = p[4]; - v->rgba[2] = p[5]; - v->rgba[3] = p[6]; - return 7; - } -} - -static void gl2psParseFeedbackBuffer(GLint used) { - char flag; - GLushort pattern = 0; - GLboolean boundary; - GLint i, sizeoffloat, count, v, vtot, offset = 0, factor = 0, auxindex = 0; - GLfloat lwidth = 1.0F, psize = 1.0F; - GLfloat *current; - GL2PSvertex vertices[3]; - GL2PSprimitive *prim; - GL2PSimagemap *node; - - current = gl2ps->feedback; - boundary = gl2ps->boundary = GL_FALSE; - - while (used > 0) { - - if (GL_TRUE == boundary) - gl2ps->boundary = GL_TRUE; - - switch ((GLint) * current) { - case GL_POINT_TOKEN: - current++; - used--; - i = gl2psGetVertex(&vertices[0], current); - current += i; - used -= i; - gl2psAddPolyPrimitive(GL2PS_POINT, 1, vertices, 0, pattern, factor, - psize, 0); - break; - case GL_LINE_TOKEN: - case GL_LINE_RESET_TOKEN: - current++; - used--; - i = gl2psGetVertex(&vertices[0], current); - current += i; - used -= i; - i = gl2psGetVertex(&vertices[1], current); - current += i; - used -= i; - gl2psAddPolyPrimitive(GL2PS_LINE, 2, vertices, 0, pattern, factor, - lwidth, 0); - break; - case GL_POLYGON_TOKEN: - count = (GLint) current[1]; - current += 2; - used -= 2; - v = vtot = 0; - while (count > 0 && used > 0) { - i = gl2psGetVertex(&vertices[v], current); - gl2psAdaptVertexForBlending(&vertices[v]); - current += i; - used -= i; - count--; - vtot++; - if (v == 2) { - if (GL_TRUE == boundary) { + gl2psListAdd(gl2ps->primitives, &prim) ; +} + +static GLint gl2psGetVertex(GL2PSvertex *v, GLfloat *p) +{ + GLint i ; + + v->xyz[0] = p[0] ; + v->xyz[1] = p[1] ; + v->xyz[2] = p[2] ; + + if (gl2ps->colormode == GL_COLOR_INDEX && gl2ps->colorsize > 0) + { + i = (GLint)(p[3] + 0.5) ; + v->rgba[0] = gl2ps->colormap[i][0] ; + v->rgba[1] = gl2ps->colormap[i][1] ; + v->rgba[2] = gl2ps->colormap[i][2] ; + v->rgba[3] = gl2ps->colormap[i][3] ; + return 4 ; + } + else + { + v->rgba[0] = p[3] ; + v->rgba[1] = p[4] ; + v->rgba[2] = p[5] ; + v->rgba[3] = p[6] ; + return 7 ; + } +} + +static void gl2psParseFeedbackBuffer(GLint used) +{ + char flag ; + GLushort pattern = 0 ; + GLboolean boundary ; + GLint i, sizeoffloat, count, v, vtot, offset = 0, factor = 0, auxindex = 0 ; + GLfloat lwidth = 1.0F, psize = 1.0F ; + GLfloat *current ; + GL2PSvertex vertices[3] ; + GL2PSprimitive *prim ; + GL2PSimagemap *node ; + + current = gl2ps->feedback ; + boundary = gl2ps->boundary = GL_FALSE ; + + while (used > 0) + { + + if (GL_TRUE == boundary) gl2ps->boundary = GL_TRUE ; + + switch ((GLint) * current) + { + case GL_POINT_TOKEN : + current++ ; + used-- ; + i = gl2psGetVertex(&vertices[0], current) ; + current += i ; + used -= i ; + gl2psAddPolyPrimitive(GL2PS_POINT, 1, vertices, 0, pattern, factor, psize, 0) ; + break ; + case GL_LINE_TOKEN : + case GL_LINE_RESET_TOKEN : + current++ ; + used-- ; + i = gl2psGetVertex(&vertices[0], current) ; + current += i ; + used -= i ; + i = gl2psGetVertex(&vertices[1], current) ; + current += i ; + used -= i ; + gl2psAddPolyPrimitive(GL2PS_LINE, 2, vertices, 0, pattern, factor, lwidth, 0) ; + break ; + case GL_POLYGON_TOKEN : + count = (GLint)current[1] ; + current += 2 ; + used -= 2 ; + v = vtot = 0 ; + while (count > 0 && used > 0) + { + i = gl2psGetVertex(&vertices[v], current) ; + gl2psAdaptVertexForBlending(&vertices[v]) ; + current += i ; + used -= i ; + count-- ; + vtot++ ; + if (v == 2) + { + if (GL_TRUE == boundary) + { if (!count && vtot == 2) - flag = 1 | 2 | 4; + flag = 1 | 2 | 4 ; else if (!count) - flag = 2 | 4; + flag = 2 | 4 ; else if (vtot == 2) - flag = 1 | 2; + flag = 1 | 2 ; else - flag = 2; - } else - flag = 0; - gl2psAddPolyPrimitive(GL2PS_TRIANGLE, 3, vertices, offset, - pattern, factor, 1, flag); - vertices[1] = vertices[2]; - } else - v++; + flag = 2 ; + } + else + flag = 0 ; + gl2psAddPolyPrimitive(GL2PS_TRIANGLE, 3, vertices, offset, pattern, factor, 1, + flag) ; + vertices[1] = vertices[2] ; + } + else + v++ ; } - break; - case GL_BITMAP_TOKEN: - case GL_DRAW_PIXEL_TOKEN: - case GL_COPY_PIXEL_TOKEN: - current++; - used--; - i = gl2psGetVertex(&vertices[0], current); - current += i; - used -= i; - break; - case GL_PASS_THROUGH_TOKEN: - switch ((GLint) current[1]) { - case GL2PS_BEGIN_OFFSET_TOKEN: - offset = 1; - break; - case GL2PS_END_OFFSET_TOKEN: - offset = 0; - break; - case GL2PS_BEGIN_BOUNDARY_TOKEN: - boundary = GL_TRUE; - break; - case GL2PS_END_BOUNDARY_TOKEN: - boundary = GL_FALSE; - break; - case GL2PS_END_STIPPLE_TOKEN: - pattern = factor = 0; - break; - case GL2PS_BEGIN_BLEND_TOKEN: - gl2ps->blending = GL_TRUE; - break; - case GL2PS_END_BLEND_TOKEN: - gl2ps->blending = GL_FALSE; - break; - case GL2PS_BEGIN_STIPPLE_TOKEN: - current += 2; - used -= 2; - pattern = (GLushort) current[1]; - current += 2; - used -= 2; - factor = (GLint) current[1]; - break; - case GL2PS_SRC_BLEND_TOKEN: - current += 2; - used -= 2; - gl2ps->blendfunc[0] = (GLint) current[1]; - break; - case GL2PS_DST_BLEND_TOKEN: - current += 2; - used -= 2; - gl2ps->blendfunc[1] = (GLint) current[1]; - break; - case GL2PS_POINT_SIZE_TOKEN: - current += 2; - used -= 2; - psize = current[1]; - break; - case GL2PS_LINE_WIDTH_TOKEN: - current += 2; - used -= 2; - lwidth = current[1]; - break; - case GL2PS_IMAGEMAP_TOKEN: - prim = (GL2PSprimitive *) gl2psMalloc(sizeof(GL2PSprimitive)); - prim->type = GL2PS_IMAGEMAP; - prim->boundary = 0; - prim->numverts = 4; - prim->verts = (GL2PSvertex *) gl2psMalloc( - 4 * sizeof(GL2PSvertex)); - prim->culled = 0; - prim->offset = 0; - prim->pattern = 0; - prim->factor = 0; - prim->width = 1; - - node = (GL2PSimagemap*) gl2psMalloc(sizeof(GL2PSimagemap)); - node->image = (GL2PSimage*) gl2psMalloc(sizeof(GL2PSimage)); - node->image->type = 0; - node->image->format = 0; - node->next = NULL; + break ; + case GL_BITMAP_TOKEN : + case GL_DRAW_PIXEL_TOKEN : + case GL_COPY_PIXEL_TOKEN : + current++ ; + used-- ; + i = gl2psGetVertex(&vertices[0], current) ; + current += i ; + used -= i ; + break ; + case GL_PASS_THROUGH_TOKEN : + switch ((GLint)current[1]) + { + case GL2PS_BEGIN_OFFSET_TOKEN : + offset = 1 ; + break ; + case GL2PS_END_OFFSET_TOKEN : + offset = 0 ; + break ; + case GL2PS_BEGIN_BOUNDARY_TOKEN : + boundary = GL_TRUE ; + break ; + case GL2PS_END_BOUNDARY_TOKEN : + boundary = GL_FALSE ; + break ; + case GL2PS_END_STIPPLE_TOKEN : + pattern = factor = 0 ; + break ; + case GL2PS_BEGIN_BLEND_TOKEN : + gl2ps->blending = GL_TRUE ; + break ; + case GL2PS_END_BLEND_TOKEN : + gl2ps->blending = GL_FALSE ; + break ; + case GL2PS_BEGIN_STIPPLE_TOKEN : + current += 2 ; + used -= 2 ; + pattern = (GLushort)current[1] ; + current += 2 ; + used -= 2 ; + factor = (GLint)current[1] ; + break ; + case GL2PS_SRC_BLEND_TOKEN : + current += 2 ; + used -= 2 ; + gl2ps->blendfunc[0] = (GLint)current[1] ; + break ; + case GL2PS_DST_BLEND_TOKEN : + current += 2 ; + used -= 2 ; + gl2ps->blendfunc[1] = (GLint)current[1] ; + break ; + case GL2PS_POINT_SIZE_TOKEN : + current += 2 ; + used -= 2 ; + psize = current[1] ; + break ; + case GL2PS_LINE_WIDTH_TOKEN : + current += 2 ; + used -= 2 ; + lwidth = current[1] ; + break ; + case GL2PS_IMAGEMAP_TOKEN : + prim = (GL2PSprimitive *)gl2psMalloc(sizeof(GL2PSprimitive)) ; + prim->type = GL2PS_IMAGEMAP ; + prim->boundary = 0 ; + prim->numverts = 4 ; + prim->verts = (GL2PSvertex *)gl2psMalloc(4 * sizeof(GL2PSvertex)) ; + prim->culled = 0 ; + prim->offset = 0 ; + prim->pattern = 0 ; + prim->factor = 0 ; + prim->width = 1 ; + + node = (GL2PSimagemap*)gl2psMalloc(sizeof(GL2PSimagemap)) ; + node->image = (GL2PSimage*)gl2psMalloc(sizeof(GL2PSimage)) ; + node->image->type = 0 ; + node->image->format = 0 ; + node->next = NULL ; if (gl2ps->imagemap_head == NULL) - gl2ps->imagemap_head = node; + gl2ps->imagemap_head = node ; else - gl2ps->imagemap_tail->next = node; - gl2ps->imagemap_tail = node; - prim->data.image = node->image; - - current += 2; - used -= 2; - i = gl2psGetVertex(&prim->verts[0], ¤t[1]); - current += i; - used -= i; - - node->image->width = (GLint) current[2]; - current += 2; - used -= 2; - node->image->height = (GLint) current[2]; - prim->verts[0].xyz[0] = prim->verts[0].xyz[0] - - (int) (node->image->width / 2) + 0.5F; - prim->verts[0].xyz[1] = prim->verts[0].xyz[1] - - (int) (node->image->height / 2) + 0.5F; - for (i = 1; i < 4; i++) { - for (v = 0; v < 3; v++) { - prim->verts[i].xyz[v] = prim->verts[0].xyz[v]; - prim->verts[i].rgba[v] = prim->verts[0].rgba[v]; + gl2ps->imagemap_tail->next = node ; + gl2ps->imagemap_tail = node ; + prim->data.image = node->image ; + + current += 2 ; + used -= 2 ; + i = gl2psGetVertex(&prim->verts[0], ¤t[1]) ; + current += i ; + used -= i ; + + node->image->width = (GLint)current[2] ; + current += 2 ; + used -= 2 ; + node->image->height = (GLint)current[2] ; + prim->verts[0].xyz[0] = prim->verts[0].xyz[0] - (int)(node->image->width / 2) + + 0.5F ; + prim->verts[0].xyz[1] = prim->verts[0].xyz[1] - (int)(node->image->height / 2) + + 0.5F ; + for (i = 1 ; i < 4; i++) + { + for (v = 0 ; v < 3; v++) + { + prim->verts[i].xyz[v] = prim->verts[0].xyz[v] ; + prim->verts[i].rgba[v] = prim->verts[0].rgba[v] ; } - prim->verts[i].rgba[v] = prim->verts[0].rgba[v]; + prim->verts[i].rgba[v] = prim->verts[0].rgba[v] ; } - prim->verts[1].xyz[0] = prim->verts[1].xyz[0] - + node->image->width; - prim->verts[2].xyz[0] = prim->verts[1].xyz[0]; - prim->verts[2].xyz[1] = prim->verts[2].xyz[1] - + node->image->height; - prim->verts[3].xyz[1] = prim->verts[2].xyz[1]; - - sizeoffloat = sizeof(GLfloat); - v = 2 * sizeoffloat; - vtot = node->image->height - + node->image->height * ((node->image->width - 1) / 8); - node->image->pixels = (GLfloat*) gl2psMalloc(v + vtot); - node->image->pixels[0] = prim->verts[0].xyz[0]; - node->image->pixels[1] = prim->verts[0].xyz[1]; - - for (i = 0; i < vtot; i += sizeoffloat) { - current += 2; - used -= 2; + prim->verts[1].xyz[0] = prim->verts[1].xyz[0] + node->image->width ; + prim->verts[2].xyz[0] = prim->verts[1].xyz[0] ; + prim->verts[2].xyz[1] = prim->verts[2].xyz[1] + node->image->height ; + prim->verts[3].xyz[1] = prim->verts[2].xyz[1] ; + + sizeoffloat = sizeof(GLfloat) ; + v = 2 * sizeoffloat ; + vtot = node->image->height + node->image->height * ((node->image->width - 1) / 8) ; + node->image->pixels = (GLfloat*)gl2psMalloc(v + vtot) ; + node->image->pixels[0] = prim->verts[0].xyz[0] ; + node->image->pixels[1] = prim->verts[0].xyz[1] ; + + for (i = 0 ; i < vtot; i += sizeoffloat) + { + current += 2 ; + used -= 2 ; if ((vtot - i) >= 4) - memcpy(&(((char*) (node->image->pixels))[i + v]), - &(current[2]), sizeoffloat); + memcpy(&(((char*)(node->image->pixels))[i + v]), &(current[2]), + sizeoffloat) ; else - memcpy(&(((char*) (node->image->pixels))[i + v]), - &(current[2]), vtot - i); + memcpy(&(((char*)(node->image->pixels))[i + v]), &(current[2]), vtot - i) ; } - current++; - used--; - gl2psListAdd(gl2ps->primitives, &prim); - break; - case GL2PS_DRAW_PIXELS_TOKEN: - case GL2PS_TEXT_TOKEN: + current++ ; + used-- ; + gl2psListAdd(gl2ps->primitives, &prim) ; + break ; + case GL2PS_DRAW_PIXELS_TOKEN : + case GL2PS_TEXT_TOKEN : if (auxindex < gl2psListNbr(gl2ps->auxprimitives)) gl2psListAdd(gl2ps->primitives, - gl2psListPointer(gl2ps->auxprimitives, auxindex++)); + gl2psListPointer(gl2ps->auxprimitives, auxindex++)) ; else - gl2psMsg(GL2PS_ERROR, - "Wrong number of auxiliary tokens in buffer"); - break; + gl2psMsg(GL2PS_ERROR, "Wrong number of auxiliary tokens in buffer") ; + break ; } - current += 2; - used -= 2; - break; - default: - gl2psMsg(GL2PS_WARNING, "Unknown token in buffer"); - current++; - used--; - break; + current += 2 ; + used -= 2 ; + break ; + default : + gl2psMsg(GL2PS_WARNING, "Unknown token in buffer") ; + current++ ; + used-- ; + break ; } } - gl2psListReset(gl2ps->auxprimitives); + gl2psListReset(gl2ps->auxprimitives) ; } /********************************************************************* @@ -2414,270 +2629,312 @@ static void gl2psParseFeedbackBuffer(GLint used) { * *********************************************************************/ -static void gl2psWriteByte(unsigned char byte) { - unsigned char h = byte / 16; - unsigned char l = byte % 16; - gl2psPrintf("%x%x", h, l); +static void gl2psWriteByte(unsigned char byte) +{ + unsigned char h = byte / 16 ; + unsigned char l = byte % 16 ; + gl2psPrintf("%x%x", h, l) ; } -static void gl2psPrintPostScriptPixmap(GLfloat x, GLfloat y, GL2PSimage *im) { - GLuint nbhex, nbyte, nrgb, nbits; - GLuint row, col, ibyte, icase; - GLfloat dr, dg, db, fgrey; - unsigned char red = 0, green = 0, blue = 0, b, grey; - GLuint width = (GLuint) im->width; - GLuint height = (GLuint) im->height; +static void gl2psPrintPostScriptPixmap(GLfloat x, GLfloat y, GL2PSimage *im) +{ + GLuint nbhex, nbyte, nrgb, nbits ; + GLuint row, col, ibyte, icase ; + GLfloat dr, dg, db, fgrey ; + unsigned char red = 0, green = 0, blue = 0, b, grey ; + GLuint width = (GLuint)im->width ; + GLuint height = (GLuint)im->height ; /* FIXME: should we define an option for these? Or just keep the 8-bit per component case? */ - int greyscale = 0; /* set to 1 to output greyscale image */ - int nbit = 8; /* number of bits per color compoment (2, 4 or 8) */ - - if ((width <= 0) || (height <= 0)) - return; - - gl2psPrintf("gsave\n"); - gl2psPrintf("%.2f %.2f translate\n", x, y); - gl2psPrintf("%d %d scale\n", width, height); - - if (greyscale) { /* greyscale */ - gl2psPrintf("/picstr %d string def\n", width); - gl2psPrintf("%d %d %d\n", width, height, 8); - gl2psPrintf("[ %d 0 0 -%d 0 %d ]\n", width, height, height); - gl2psPrintf("{ currentfile picstr readhexstring pop }\n"); - gl2psPrintf("image\n"); - for (row = 0; row < height; row++) { - for (col = 0; col < width; col++) { - gl2psGetRGB(im, col, row, &dr, &dg, &db); - fgrey = (0.30F * dr + 0.59F * dg + 0.11F * db); - grey = (unsigned char) (255. * fgrey); - gl2psWriteByte(grey); + int greyscale = 0 ; /* set to 1 to output greyscale image */ + int nbit = 8 ; /* number of bits per color compoment (2, 4 or 8) */ + + if ((width <= 0) || (height <= 0)) return ; + + gl2psPrintf("gsave\n") ; + gl2psPrintf("%.2f %.2f translate\n", x, y) ; + gl2psPrintf("%d %d scale\n", width, height) ; + + if (greyscale) + { /* greyscale */ + gl2psPrintf("/picstr %d string def\n", width) ; + gl2psPrintf("%d %d %d\n", width, height, 8) ; + gl2psPrintf("[ %d 0 0 -%d 0 %d ]\n", width, height, height) ; + gl2psPrintf("{ currentfile picstr readhexstring pop }\n") ; + gl2psPrintf("image\n") ; + for (row = 0 ; row < height; row++) + { + for (col = 0 ; col < width; col++) + { + gl2psGetRGB(im, col, row, &dr, &dg, &db) ; + fgrey = (0.30F * dr + 0.59F * dg + 0.11F * db) ; + grey = (unsigned char)(255. * fgrey) ; + gl2psWriteByte(grey) ; } - gl2psPrintf("\n"); + gl2psPrintf("\n") ; } - nbhex = width * height * 2; - gl2psPrintf("%%%% nbhex digit :%d\n", nbhex); - } else if (nbit == 2) { /* color, 2 bits for r and g and b; rgbs following each other */ - nrgb = width * 3; - nbits = nrgb * nbit; - nbyte = nbits / 8; - if ((nbyte * 8) != nbits) - nbyte++; - gl2psPrintf("/rgbstr %d string def\n", nbyte); - gl2psPrintf("%d %d %d\n", width, height, nbit); - gl2psPrintf("[ %d 0 0 -%d 0 %d ]\n", width, height, height); - gl2psPrintf("{ currentfile rgbstr readhexstring pop }\n"); - gl2psPrintf("false 3\n"); - gl2psPrintf("colorimage\n"); - for (row = 0; row < height; row++) { - icase = 1; - col = 0; - b = 0; - for (ibyte = 0; ibyte < nbyte; ibyte++) { - if (icase == 1) { - if (col < width) { - gl2psGetRGB(im, col, row, &dr, &dg, &db); - } else { - dr = dg = db = 0; + nbhex = width * height * 2 ; + gl2psPrintf("%%%% nbhex digit :%d\n", nbhex) ; + } + else if (nbit == 2) + { /* color, 2 bits for r and g and b; rgbs following each other */ + nrgb = width * 3 ; + nbits = nrgb * nbit ; + nbyte = nbits / 8 ; + if ((nbyte * 8) != nbits) nbyte++ ; + gl2psPrintf("/rgbstr %d string def\n", nbyte) ; + gl2psPrintf("%d %d %d\n", width, height, nbit) ; + gl2psPrintf("[ %d 0 0 -%d 0 %d ]\n", width, height, height) ; + gl2psPrintf("{ currentfile rgbstr readhexstring pop }\n") ; + gl2psPrintf("false 3\n") ; + gl2psPrintf("colorimage\n") ; + for (row = 0 ; row < height; row++) + { + icase = 1 ; + col = 0 ; + b = 0 ; + for (ibyte = 0 ; ibyte < nbyte; ibyte++) + { + if (icase == 1) + { + if (col < width) + { + gl2psGetRGB(im, col, row, &dr, &dg, &db) ; } - col++; - red = (unsigned char) (3. * dr); - green = (unsigned char) (3. * dg); - blue = (unsigned char) (3. * db); - b = red; - b = (b << 2) + green; - b = (b << 2) + blue; - if (col < width) { - gl2psGetRGB(im, col, row, &dr, &dg, &db); - } else { - dr = dg = db = 0; + else + { + dr = dg = db = 0 ; } - col++; - red = (unsigned char) (3. * dr); - green = (unsigned char) (3. * dg); - blue = (unsigned char) (3. * db); - b = (b << 2) + red; - gl2psWriteByte(b); - b = 0; - icase++; - } else if (icase == 2) { - b = green; - b = (b << 2) + blue; - if (col < width) { - gl2psGetRGB(im, col, row, &dr, &dg, &db); - } else { - dr = dg = db = 0; + col++ ; + red = (unsigned char)(3. * dr) ; + green = (unsigned char)(3. * dg) ; + blue = (unsigned char)(3. * db) ; + b = red ; + b = (b << 2) + green ; + b = (b << 2) + blue ; + if (col < width) + { + gl2psGetRGB(im, col, row, &dr, &dg, &db) ; } - col++; - red = (unsigned char) (3. * dr); - green = (unsigned char) (3. * dg); - blue = (unsigned char) (3. * db); - b = (b << 2) + red; - b = (b << 2) + green; - gl2psWriteByte(b); - b = 0; - icase++; - } else if (icase == 3) { - b = blue; - if (col < width) { - gl2psGetRGB(im, col, row, &dr, &dg, &db); - } else { - dr = dg = db = 0; + else + { + dr = dg = db = 0 ; + } + col++ ; + red = (unsigned char)(3. * dr) ; + green = (unsigned char)(3. * dg) ; + blue = (unsigned char)(3. * db) ; + b = (b << 2) + red ; + gl2psWriteByte(b) ; + b = 0 ; + icase++ ; + } + else if (icase == 2) + { + b = green ; + b = (b << 2) + blue ; + if (col < width) + { + gl2psGetRGB(im, col, row, &dr, &dg, &db) ; + } + else + { + dr = dg = db = 0 ; + } + col++ ; + red = (unsigned char)(3. * dr) ; + green = (unsigned char)(3. * dg) ; + blue = (unsigned char)(3. * db) ; + b = (b << 2) + red ; + b = (b << 2) + green ; + gl2psWriteByte(b) ; + b = 0 ; + icase++ ; + } + else if (icase == 3) + { + b = blue ; + if (col < width) + { + gl2psGetRGB(im, col, row, &dr, &dg, &db) ; } - col++; - red = (unsigned char) (3. * dr); - green = (unsigned char) (3. * dg); - blue = (unsigned char) (3. * db); - b = (b << 2) + red; - b = (b << 2) + green; - b = (b << 2) + blue; - gl2psWriteByte(b); - b = 0; - icase = 1; + else + { + dr = dg = db = 0 ; + } + col++ ; + red = (unsigned char)(3. * dr) ; + green = (unsigned char)(3. * dg) ; + blue = (unsigned char)(3. * db) ; + b = (b << 2) + red ; + b = (b << 2) + green ; + b = (b << 2) + blue ; + gl2psWriteByte(b) ; + b = 0 ; + icase = 1 ; } } - gl2psPrintf("\n"); + gl2psPrintf("\n") ; } - } else if (nbit == 4) { /* color, 4 bits for r and g and b; rgbs following each other */ - nrgb = width * 3; - nbits = nrgb * nbit; - nbyte = nbits / 8; - if ((nbyte * 8) != nbits) - nbyte++; - gl2psPrintf("/rgbstr %d string def\n", nbyte); - gl2psPrintf("%d %d %d\n", width, height, nbit); - gl2psPrintf("[ %d 0 0 -%d 0 %d ]\n", width, height, height); - gl2psPrintf("{ currentfile rgbstr readhexstring pop }\n"); - gl2psPrintf("false 3\n"); - gl2psPrintf("colorimage\n"); - for (row = 0; row < height; row++) { - col = 0; - icase = 1; - for (ibyte = 0; ibyte < nbyte; ibyte++) { - if (icase == 1) { - if (col < width) { - gl2psGetRGB(im, col, row, &dr, &dg, &db); - } else { - dr = dg = db = 0; + } + else if (nbit == 4) + { /* color, 4 bits for r and g and b; rgbs following each other */ + nrgb = width * 3 ; + nbits = nrgb * nbit ; + nbyte = nbits / 8 ; + if ((nbyte * 8) != nbits) nbyte++ ; + gl2psPrintf("/rgbstr %d string def\n", nbyte) ; + gl2psPrintf("%d %d %d\n", width, height, nbit) ; + gl2psPrintf("[ %d 0 0 -%d 0 %d ]\n", width, height, height) ; + gl2psPrintf("{ currentfile rgbstr readhexstring pop }\n") ; + gl2psPrintf("false 3\n") ; + gl2psPrintf("colorimage\n") ; + for (row = 0 ; row < height; row++) + { + col = 0 ; + icase = 1 ; + for (ibyte = 0 ; ibyte < nbyte; ibyte++) + { + if (icase == 1) + { + if (col < width) + { + gl2psGetRGB(im, col, row, &dr, &dg, &db) ; + } + else + { + dr = dg = db = 0 ; + } + col++ ; + red = (unsigned char)(15. * dr) ; + green = (unsigned char)(15. * dg) ; + gl2psPrintf("%x%x", red, green) ; + icase++ ; + } + else if (icase == 2) + { + blue = (unsigned char)(15. * db) ; + if (col < width) + { + gl2psGetRGB(im, col, row, &dr, &dg, &db) ; } - col++; - red = (unsigned char) (15. * dr); - green = (unsigned char) (15. * dg); - gl2psPrintf("%x%x", red, green); - icase++; - } else if (icase == 2) { - blue = (unsigned char) (15. * db); - if (col < width) { - gl2psGetRGB(im, col, row, &dr, &dg, &db); - } else { - dr = dg = db = 0; + else + { + dr = dg = db = 0 ; } - col++; - red = (unsigned char) (15. * dr); - gl2psPrintf("%x%x", blue, red); - icase++; - } else if (icase == 3) { - green = (unsigned char) (15. * dg); - blue = (unsigned char) (15. * db); - gl2psPrintf("%x%x", green, blue); - icase = 1; + col++ ; + red = (unsigned char)(15. * dr) ; + gl2psPrintf("%x%x", blue, red) ; + icase++ ; + } + else if (icase == 3) + { + green = (unsigned char)(15. * dg) ; + blue = (unsigned char)(15. * db) ; + gl2psPrintf("%x%x", green, blue) ; + icase = 1 ; } } - gl2psPrintf("\n"); + gl2psPrintf("\n") ; } - } else { /* 8 bit for r and g and b */ - nbyte = width * 3; - gl2psPrintf("/rgbstr %d string def\n", nbyte); - gl2psPrintf("%d %d %d\n", width, height, 8); - gl2psPrintf("[ %d 0 0 -%d 0 %d ]\n", width, height, height); - gl2psPrintf("{ currentfile rgbstr readhexstring pop }\n"); - gl2psPrintf("false 3\n"); - gl2psPrintf("colorimage\n"); - for (row = 0; row < height; row++) { - for (col = 0; col < width; col++) { - gl2psGetRGB(im, col, row, &dr, &dg, &db); - red = (unsigned char) (255. * dr); - gl2psWriteByte(red); - green = (unsigned char) (255. * dg); - gl2psWriteByte(green); - blue = (unsigned char) (255. * db); - gl2psWriteByte(blue); + } + else + { /* 8 bit for r and g and b */ + nbyte = width * 3 ; + gl2psPrintf("/rgbstr %d string def\n", nbyte) ; + gl2psPrintf("%d %d %d\n", width, height, 8) ; + gl2psPrintf("[ %d 0 0 -%d 0 %d ]\n", width, height, height) ; + gl2psPrintf("{ currentfile rgbstr readhexstring pop }\n") ; + gl2psPrintf("false 3\n") ; + gl2psPrintf("colorimage\n") ; + for (row = 0 ; row < height; row++) + { + for (col = 0 ; col < width; col++) + { + gl2psGetRGB(im, col, row, &dr, &dg, &db) ; + red = (unsigned char)(255. * dr) ; + gl2psWriteByte(red) ; + green = (unsigned char)(255. * dg) ; + gl2psWriteByte(green) ; + blue = (unsigned char)(255. * db) ; + gl2psWriteByte(blue) ; } - gl2psPrintf("\n"); + gl2psPrintf("\n") ; } } - gl2psPrintf("grestore\n"); + gl2psPrintf("grestore\n") ; } -static void gl2psPrintPostScriptImagemap(GLfloat x, GLfloat y, GLsizei width, - GLsizei height, const unsigned char *imagemap) { - int i, size; +static void gl2psPrintPostScriptImagemap(GLfloat x, GLfloat y, GLsizei width, GLsizei height, + const unsigned char *imagemap) +{ + int i, size ; - if ((width <= 0) || (height <= 0)) - return; + if ((width <= 0) || (height <= 0)) return ; - size = height + height * (width - 1) / 8; + size = height + height * (width - 1) / 8 ; - gl2psPrintf("gsave\n"); - gl2psPrintf("%.2f %.2f translate\n", x, y); - gl2psPrintf("%d %d scale\n%d %d\ntrue\n", width, height, width, height); - gl2psPrintf("[ %d 0 0 -%d 0 %d ] {<", width, height); - for (i = 0; i < size; i++) { - gl2psWriteByte(*imagemap); - imagemap++; + gl2psPrintf("gsave\n") ; + gl2psPrintf("%.2f %.2f translate\n", x, y) ; + gl2psPrintf("%d %d scale\n%d %d\ntrue\n", width, height, width, height) ; + gl2psPrintf("[ %d 0 0 -%d 0 %d ] {<", width, height) ; + for (i = 0 ; i < size; i++) + { + gl2psWriteByte(*imagemap) ; + imagemap++ ; } - gl2psPrintf(">} imagemask\ngrestore\n"); + gl2psPrintf(">} imagemask\ngrestore\n") ; } -static void gl2psPrintPostScriptHeader(void) { - time_t now; +static void gl2psPrintPostScriptHeader(void) +{ + time_t now ; /* Since compression is not part of the PostScript standard, compressed PostScript files are just gzipped PostScript files ("ps.gz" or "eps.gz") */ - gl2psPrintGzipHeader(); + gl2psPrintGzipHeader() ; - time(&now); + time(&now) ; - if (gl2ps->format == GL2PS_PS) { - gl2psPrintf("%%!PS-Adobe-3.0\n"); - } else { - gl2psPrintf("%%!PS-Adobe-3.0 EPSF-3.0\n"); + if (gl2ps->format == GL2PS_PS) + { + gl2psPrintf("%%!PS-Adobe-3.0\n") ; + } + else + { + gl2psPrintf("%%!PS-Adobe-3.0 EPSF-3.0\n") ; } gl2psPrintf("%%%%Title: %s\n" - "%%%%Creator: GL2PS %d.%d.%d%s, %s\n" - "%%%%For: %s\n" - "%%%%CreationDate: %s" - "%%%%LanguageLevel: 3\n" - "%%%%DocumentData: Clean7Bit\n" - "%%%%Pages: 1\n", gl2ps->title, GL2PS_MAJOR_VERSION, - GL2PS_MINOR_VERSION, GL2PS_PATCH_VERSION, GL2PS_EXTRA_VERSION, - GL2PS_COPYRIGHT, gl2ps->producer, ctime(&now)); - - if (gl2ps->format == GL2PS_PS) { + "%%%%Creator: GL2PS %d.%d.%d%s, %s\n" + "%%%%For: %s\n" + "%%%%CreationDate: %s" + "%%%%LanguageLevel: 3\n" + "%%%%DocumentData: Clean7Bit\n" + "%%%%Pages: 1\n", + gl2ps->title, GL2PS_MAJOR_VERSION, GL2PS_MINOR_VERSION, GL2PS_PATCH_VERSION, + GL2PS_EXTRA_VERSION, GL2PS_COPYRIGHT, gl2ps->producer, ctime(&now)) ; + + if (gl2ps->format == GL2PS_PS) + { gl2psPrintf( - "%%%%Orientation: %s\n" - "%%%%DocumentMedia: Default %d %d 0 () ()\n", - (gl2ps->options & GL2PS_LANDSCAPE) ? "Landscape" : "Portrait", - (gl2ps->options & GL2PS_LANDSCAPE) ? - (int) gl2ps->viewport[3] : (int) gl2ps->viewport[2], - (gl2ps->options & GL2PS_LANDSCAPE) ? - (int) gl2ps->viewport[2] : (int) gl2ps->viewport[3]); + "%%%%Orientation: %s\n" + "%%%%DocumentMedia: Default %d %d 0 () ()\n", + (gl2ps->options & GL2PS_LANDSCAPE) ? "Landscape" : "Portrait", + (gl2ps->options & GL2PS_LANDSCAPE) ? (int)gl2ps->viewport[3] : (int)gl2ps->viewport[2], + (gl2ps->options & GL2PS_LANDSCAPE) ? (int)gl2ps->viewport[2] : (int)gl2ps->viewport[3]) ; } gl2psPrintf( - "%%%%BoundingBox: %d %d %d %d\n" - "%%%%EndComments\n", - (gl2ps->options & GL2PS_LANDSCAPE) ? - (int) gl2ps->viewport[1] : (int) gl2ps->viewport[0], - (gl2ps->options & GL2PS_LANDSCAPE) ? - (int) gl2ps->viewport[0] : (int) gl2ps->viewport[1], - (gl2ps->options & GL2PS_LANDSCAPE) ? - (int) gl2ps->viewport[3] : (int) gl2ps->viewport[2], - (gl2ps->options & GL2PS_LANDSCAPE) ? - (int) gl2ps->viewport[2] : (int) gl2ps->viewport[3]); + "%%%%BoundingBox: %d %d %d %d\n" + "%%%%EndComments\n", + (gl2ps->options & GL2PS_LANDSCAPE) ? (int)gl2ps->viewport[1] : (int)gl2ps->viewport[0], + (gl2ps->options & GL2PS_LANDSCAPE) ? (int)gl2ps->viewport[0] : (int)gl2ps->viewport[1], + (gl2ps->options & GL2PS_LANDSCAPE) ? (int)gl2ps->viewport[3] : (int)gl2ps->viewport[2], + (gl2ps->options & GL2PS_LANDSCAPE) ? (int)gl2ps->viewport[2] : (int)gl2ps->viewport[3]) ; /* RGB color: r g b C (replace C by G in output to change from rgb to gray) Grayscale: r g b G @@ -2693,65 +2950,61 @@ static void gl2psPrintPostScriptHeader(void) { Smooth-shaded triangle: x3 y3 r3 g3 b3 x2 y2 r2 g2 b2 x1 y1 r1 g1 b1 ST */ gl2psPrintf("%%%%BeginProlog\n" - "/gl2psdict 64 dict def gl2psdict begin\n" - "0 setlinecap 0 setlinejoin\n" - "/tryPS3shading %s def %% set to false to force subdivision\n" - "/rThreshold %g def %% red component subdivision threshold\n" - "/gThreshold %g def %% green component subdivision threshold\n" - "/bThreshold %g def %% blue component subdivision threshold\n", - (gl2ps->options & GL2PS_NO_PS3_SHADING) ? "false" : "true", - gl2ps->threshold[0], gl2ps->threshold[1], gl2ps->threshold[2]); - - gl2psPrintf( - "/BD { bind def } bind def\n" - "/C { setrgbcolor } BD\n" - "/G { 0.082 mul exch 0.6094 mul add exch 0.3086 mul add neg 1.0 add setgray } BD\n" - "/W { setlinewidth } BD\n"); + "/gl2psdict 64 dict def gl2psdict begin\n" + "0 setlinecap 0 setlinejoin\n" + "/tryPS3shading %s def %% set to false to force subdivision\n" + "/rThreshold %g def %% red component subdivision threshold\n" + "/gThreshold %g def %% green component subdivision threshold\n" + "/bThreshold %g def %% blue component subdivision threshold\n", + (gl2ps->options & GL2PS_NO_PS3_SHADING) ? "false" : "true", gl2ps->threshold[0], + gl2ps->threshold[1], gl2ps->threshold[2]) ; + + gl2psPrintf("/BD { bind def } bind def\n" + "/C { setrgbcolor } BD\n" + "/G { 0.082 mul exch 0.6094 mul add exch 0.3086 mul add neg 1.0 add setgray } BD\n" + "/W { setlinewidth } BD\n") ; gl2psPrintf("/FC { findfont exch /SH exch def SH scalefont setfont } BD\n" - "/SW { dup stringwidth pop } BD\n" - "/S { FC moveto show } BD\n" - "/SBC{ FC moveto SW -2 div 0 rmoveto show } BD\n" - "/SBR{ FC moveto SW neg 0 rmoveto show } BD\n" - "/SCL{ FC moveto 0 SH -2 div rmoveto show } BD\n" - "/SCC{ FC moveto SW -2 div SH -2 div rmoveto show } BD\n" - "/SCR{ FC moveto SW neg SH -2 div rmoveto show } BD\n" - "/STL{ FC moveto 0 SH neg rmoveto show } BD\n" - "/STC{ FC moveto SW -2 div SH neg rmoveto show } BD\n" - "/STR{ FC moveto SW neg SH neg rmoveto show } BD\n"); + "/SW { dup stringwidth pop } BD\n" + "/S { FC moveto show } BD\n" + "/SBC{ FC moveto SW -2 div 0 rmoveto show } BD\n" + "/SBR{ FC moveto SW neg 0 rmoveto show } BD\n" + "/SCL{ FC moveto 0 SH -2 div rmoveto show } BD\n" + "/SCC{ FC moveto SW -2 div SH -2 div rmoveto show } BD\n" + "/SCR{ FC moveto SW neg SH -2 div rmoveto show } BD\n" + "/STL{ FC moveto 0 SH neg rmoveto show } BD\n" + "/STC{ FC moveto SW -2 div SH neg rmoveto show } BD\n" + "/STR{ FC moveto SW neg SH neg rmoveto show } BD\n") ; /* rotated text routines: same nameanem with R appended */ - gl2psPrintf( - "/FCT { FC translate 0 0 } BD\n" - "/SR { gsave FCT moveto rotate show grestore } BD\n" - "/SBCR{ gsave FCT moveto rotate SW -2 div 0 rmoveto show grestore } BD\n" - "/SBRR{ gsave FCT moveto rotate SW neg 0 rmoveto show grestore } BD\n" - "/SCLR{ gsave FCT moveto rotate 0 SH -2 div rmoveto show grestore} BD\n"); - gl2psPrintf( - "/SCCR{ gsave FCT moveto rotate SW -2 div SH -2 div rmoveto show grestore} BD\n" - "/SCRR{ gsave FCT moveto rotate SW neg SH -2 div rmoveto show grestore} BD\n" - "/STLR{ gsave FCT moveto rotate 0 SH neg rmoveto show grestore } BD\n" - "/STCR{ gsave FCT moveto rotate SW -2 div SH neg rmoveto show grestore } BD\n" - "/STRR{ gsave FCT moveto rotate SW neg SH neg rmoveto show grestore } BD\n"); + gl2psPrintf("/FCT { FC translate 0 0 } BD\n" + "/SR { gsave FCT moveto rotate show grestore } BD\n" + "/SBCR{ gsave FCT moveto rotate SW -2 div 0 rmoveto show grestore } BD\n" + "/SBRR{ gsave FCT moveto rotate SW neg 0 rmoveto show grestore } BD\n" + "/SCLR{ gsave FCT moveto rotate 0 SH -2 div rmoveto show grestore} BD\n") ; + gl2psPrintf("/SCCR{ gsave FCT moveto rotate SW -2 div SH -2 div rmoveto show grestore} BD\n" + "/SCRR{ gsave FCT moveto rotate SW neg SH -2 div rmoveto show grestore} BD\n" + "/STLR{ gsave FCT moveto rotate 0 SH neg rmoveto show grestore } BD\n" + "/STCR{ gsave FCT moveto rotate SW -2 div SH neg rmoveto show grestore } BD\n" + "/STRR{ gsave FCT moveto rotate SW neg SH neg rmoveto show grestore } BD\n") ; gl2psPrintf("/P { newpath 0.0 360.0 arc closepath fill } BD\n" - "/LS { newpath moveto } BD\n" - "/L { lineto } BD\n" - "/LE { lineto stroke } BD\n" - "/T { newpath moveto lineto lineto closepath fill } BD\n"); + "/LS { newpath moveto } BD\n" + "/L { lineto } BD\n" + "/LE { lineto stroke } BD\n" + "/T { newpath moveto lineto lineto closepath fill } BD\n") ; /* Smooth-shaded triangle with PostScript level 3 shfill operator: x3 y3 r3 g3 b3 x2 y2 r2 g2 b2 x1 y1 r1 g1 b1 STshfill */ - gl2psPrintf( - "/STshfill {\n" - " /b1 exch def /g1 exch def /r1 exch def /y1 exch def /x1 exch def\n" - " /b2 exch def /g2 exch def /r2 exch def /y2 exch def /x2 exch def\n" - " /b3 exch def /g3 exch def /r3 exch def /y3 exch def /x3 exch def\n" - " gsave << /ShadingType 4 /ColorSpace [/DeviceRGB]\n" - " /DataSource [ 0 x1 y1 r1 g1 b1 0 x2 y2 r2 g2 b2 0 x3 y3 r3 g3 b3 ] >>\n" - " shfill grestore } BD\n"); + gl2psPrintf("/STshfill {\n" + " /b1 exch def /g1 exch def /r1 exch def /y1 exch def /x1 exch def\n" + " /b2 exch def /g2 exch def /r2 exch def /y2 exch def /x2 exch def\n" + " /b3 exch def /g3 exch def /r3 exch def /y3 exch def /x3 exch def\n" + " gsave << /ShadingType 4 /ColorSpace [/DeviceRGB]\n" + " /DataSource [ 0 x1 y1 r1 g1 b1 0 x2 y2 r2 g2 b2 0 x3 y3 r3 g3 b3 ] >>\n" + " shfill grestore } BD\n") ; /* Flat-shaded triangle with middle color: x3 y3 r3 g3 b3 x2 y2 r2 g2 b2 x1 y1 r1 g1 b1 Tm */ @@ -2759,11 +3012,11 @@ static void gl2psPrintPostScriptHeader(void) { gl2psPrintf(/* stack : x3 y3 r3 g3 b3 x2 y2 r2 g2 b2 x1 y1 r1 g1 b1 */ "/Tm { 3 -1 roll 8 -1 roll 13 -1 roll add add 3 div\n" /* r = (r1+r2+r3)/3 */ /* stack : x3 y3 g3 b3 x2 y2 g2 b2 x1 y1 g1 b1 r */ - " 3 -1 roll 7 -1 roll 11 -1 roll add add 3 div\n" /* g = (g1+g2+g3)/3 */ - /* stack : x3 y3 b3 x2 y2 b2 x1 y1 b1 r g b */ - " 3 -1 roll 6 -1 roll 9 -1 roll add add 3 div" /* b = (b1+b2+b3)/3 */ - /* stack : x3 y3 x2 y2 x1 y1 r g b */ - " C T } BD\n"); + " 3 -1 roll 7 -1 roll 11 -1 roll add add 3 div\n" /* g = (g1+g2+g3)/3 */ + /* stack : x3 y3 b3 x2 y2 b2 x1 y1 b1 r g b */ + " 3 -1 roll 6 -1 roll 9 -1 roll add add 3 div" /* b = (b1+b2+b3)/3 */ + /* stack : x3 y3 x2 y2 x1 y1 r g b */ + " C T } BD\n") ; /* Split triangle in four sub-triangles (at sides middle points) and call the STnoshfill procedure on each, interpolating the colors in RGB space: @@ -2771,156 +3024,166 @@ static void gl2psPrintPostScriptHeader(void) { (in procedure comments key: (Vi) = xi yi ri gi bi) */ gl2psPrintf("/STsplit {\n" - " 4 index 15 index add 0.5 mul\n" /* x13 = (x1+x3)/2 */ - " 4 index 15 index add 0.5 mul\n" /* y13 = (y1+y3)/2 */ - " 4 index 15 index add 0.5 mul\n" /* r13 = (r1+r3)/2 */ - " 4 index 15 index add 0.5 mul\n" /* g13 = (g1+g3)/2 */ - " 4 index 15 index add 0.5 mul\n" /* b13 = (b1+b3)/2 */ - " 5 copy 5 copy 25 15 roll\n"); + " 4 index 15 index add 0.5 mul\n" /* x13 = (x1+x3)/2 */ + " 4 index 15 index add 0.5 mul\n" /* y13 = (y1+y3)/2 */ + " 4 index 15 index add 0.5 mul\n" /* r13 = (r1+r3)/2 */ + " 4 index 15 index add 0.5 mul\n" /* g13 = (g1+g3)/2 */ + " 4 index 15 index add 0.5 mul\n" /* b13 = (b1+b3)/2 */ + " 5 copy 5 copy 25 15 roll\n") ; /* at his point, stack = (V3) (V13) (V13) (V13) (V2) (V1) */ gl2psPrintf(" 9 index 30 index add 0.5 mul\n" /* x23 = (x2+x3)/2 */ - " 9 index 30 index add 0.5 mul\n" /* y23 = (y2+y3)/2 */ - " 9 index 30 index add 0.5 mul\n" /* r23 = (r2+r3)/2 */ - " 9 index 30 index add 0.5 mul\n" /* g23 = (g2+g3)/2 */ - " 9 index 30 index add 0.5 mul\n" /* b23 = (b2+b3)/2 */ - " 5 copy 5 copy 35 5 roll 25 5 roll 15 5 roll\n"); + " 9 index 30 index add 0.5 mul\n" /* y23 = (y2+y3)/2 */ + " 9 index 30 index add 0.5 mul\n" /* r23 = (r2+r3)/2 */ + " 9 index 30 index add 0.5 mul\n" /* g23 = (g2+g3)/2 */ + " 9 index 30 index add 0.5 mul\n" /* b23 = (b2+b3)/2 */ + " 5 copy 5 copy 35 5 roll 25 5 roll 15 5 roll\n") ; /* stack = (V3) (V13) (V23) (V13) (V23) (V13) (V23) (V2) (V1) */ gl2psPrintf(" 4 index 10 index add 0.5 mul\n" /* x12 = (x1+x2)/2 */ - " 4 index 10 index add 0.5 mul\n" /* y12 = (y1+y2)/2 */ - " 4 index 10 index add 0.5 mul\n" /* r12 = (r1+r2)/2 */ - " 4 index 10 index add 0.5 mul\n" /* g12 = (g1+g2)/2 */ - " 4 index 10 index add 0.5 mul\n" /* b12 = (b1+b2)/2 */ - " 5 copy 5 copy 40 5 roll 25 5 roll 15 5 roll 25 5 roll\n"); + " 4 index 10 index add 0.5 mul\n" /* y12 = (y1+y2)/2 */ + " 4 index 10 index add 0.5 mul\n" /* r12 = (r1+r2)/2 */ + " 4 index 10 index add 0.5 mul\n" /* g12 = (g1+g2)/2 */ + " 4 index 10 index add 0.5 mul\n" /* b12 = (b1+b2)/2 */ + " 5 copy 5 copy 40 5 roll 25 5 roll 15 5 roll 25 5 roll\n") ; /* stack = (V3) (V13) (V23) (V13) (V12) (V23) (V13) (V1) (V12) (V23) (V12) (V2) */ - gl2psPrintf(" STnoshfill STnoshfill STnoshfill STnoshfill } BD\n"); + gl2psPrintf(" STnoshfill STnoshfill STnoshfill STnoshfill } BD\n") ; /* Gouraud shaded triangle using recursive subdivision until the difference between corner colors does not exceed the thresholds: x3 y3 r3 g3 b3 x2 y2 r2 g2 b2 x1 y1 r1 g1 b1 STnoshfill */ gl2psPrintf("/STnoshfill {\n" - " 2 index 8 index sub abs rThreshold gt\n" /* |r1-r2|>rth */ - " { STsplit }\n" - " { 1 index 7 index sub abs gThreshold gt\n" /* |g1-g2|>gth */ - " { STsplit }\n" - " { dup 6 index sub abs bThreshold gt\n" /* |b1-b2|>bth */ - " { STsplit }\n" - " { 2 index 13 index sub abs rThreshold gt\n" /* |r1-r3|>rht */ - " { STsplit }\n" - " { 1 index 12 index sub abs gThreshold gt\n" /* |g1-g3|>gth */ - " { STsplit }\n" - " { dup 11 index sub abs bThreshold gt\n" /* |b1-b3|>bth */ - " { STsplit }\n" - " { 7 index 13 index sub abs rThreshold gt\n"); /* |r2-r3|>rht */ + " 2 index 8 index sub abs rThreshold gt\n" /* |r1-r2|>rth */ + " { STsplit }\n" + " { 1 index 7 index sub abs gThreshold gt\n" /* |g1-g2|>gth */ + " { STsplit }\n" + " { dup 6 index sub abs bThreshold gt\n" /* |b1-b2|>bth */ + " { STsplit }\n" + " { 2 index 13 index sub abs rThreshold gt\n" /* |r1-r3|>rht */ + " { STsplit }\n" + " { 1 index 12 index sub abs gThreshold gt\n" /* |g1-g3|>gth */ + " { STsplit }\n" + " { dup 11 index sub abs bThreshold gt\n" /* |b1-b3|>bth */ + " { STsplit }\n" + " { 7 index 13 index sub abs rThreshold gt\n") ; /* |r2-r3|>rht */ gl2psPrintf(" { STsplit }\n" - " { 6 index 12 index sub abs gThreshold gt\n" /* |g2-g3|>gth */ - " { STsplit }\n" - " { 5 index 11 index sub abs bThreshold gt\n" /* |b2-b3|>bth */ - " { STsplit }\n" - " { Tm }\n" /* all colors sufficiently similar */ - " ifelse }\n" - " ifelse }\n" - " ifelse }\n" - " ifelse }\n" - " ifelse }\n" - " ifelse }\n" - " ifelse }\n" - " ifelse }\n" - " ifelse } BD\n"); + " { 6 index 12 index sub abs gThreshold gt\n" /* |g2-g3|>gth */ + " { STsplit }\n" + " { 5 index 11 index sub abs bThreshold gt\n" /* |b2-b3|>bth */ + " { STsplit }\n" + " { Tm }\n" /* all colors sufficiently similar */ + " ifelse }\n" + " ifelse }\n" + " ifelse }\n" + " ifelse }\n" + " ifelse }\n" + " ifelse }\n" + " ifelse }\n" + " ifelse }\n" + " ifelse } BD\n") ; gl2psPrintf("tryPS3shading\n" - "{ /shfill where\n" - " { /ST { STshfill } BD }\n" - " { /ST { STnoshfill } BD }\n" - " ifelse }\n" - "{ /ST { STnoshfill } BD }\n" - "ifelse\n"); + "{ /shfill where\n" + " { /ST { STshfill } BD }\n" + " { /ST { STnoshfill } BD }\n" + " ifelse }\n" + "{ /ST { STnoshfill } BD }\n" + "ifelse\n") ; gl2psPrintf("end\n" - "%%%%EndProlog\n" - "%%%%BeginSetup\n" - "/DeviceRGB setcolorspace\n" - "gl2psdict begin\n" - "%%%%EndSetup\n" - "%%%%Page: 1 1\n" - "%%%%BeginPageSetup\n"); + "%%%%EndProlog\n" + "%%%%BeginSetup\n" + "/DeviceRGB setcolorspace\n" + "gl2psdict begin\n" + "%%%%EndSetup\n" + "%%%%Page: 1 1\n" + "%%%%BeginPageSetup\n") ; - if (gl2ps->options & GL2PS_LANDSCAPE) { - gl2psPrintf("%d 0 translate 90 rotate\n", (int) gl2ps->viewport[3]); + if (gl2ps->options & GL2PS_LANDSCAPE) + { + gl2psPrintf("%d 0 translate 90 rotate\n", (int)gl2ps->viewport[3]) ; } gl2psPrintf("%%%%EndPageSetup\n" - "mark\n" - "gsave\n" - "1.0 1.0 scale\n"); + "mark\n" + "gsave\n" + "1.0 1.0 scale\n") ; - if (gl2ps->options & GL2PS_DRAW_BACKGROUND) { + if (gl2ps->options & GL2PS_DRAW_BACKGROUND) + { gl2psPrintf("%g %g %g C\n" - "newpath %d %d moveto %d %d lineto %d %d lineto %d %d lineto\n" - "closepath fill\n", gl2ps->bgcolor[0], gl2ps->bgcolor[1], - gl2ps->bgcolor[2], (int) gl2ps->viewport[0], - (int) gl2ps->viewport[1], (int) gl2ps->viewport[2], - (int) gl2ps->viewport[1], (int) gl2ps->viewport[2], - (int) gl2ps->viewport[3], (int) gl2ps->viewport[0], - (int) gl2ps->viewport[3]); + "newpath %d %d moveto %d %d lineto %d %d lineto %d %d lineto\n" + "closepath fill\n", + gl2ps->bgcolor[0], gl2ps->bgcolor[1], gl2ps->bgcolor[2], + (int)gl2ps->viewport[0], (int)gl2ps->viewport[1], (int)gl2ps->viewport[2], + (int)gl2ps->viewport[1], (int)gl2ps->viewport[2], (int)gl2ps->viewport[3], + (int)gl2ps->viewport[0], (int)gl2ps->viewport[3]) ; } } -static void gl2psPrintPostScriptColor(GL2PSrgba rgba) { - if (!gl2psSameColor(gl2ps->lastrgba, rgba)) { - gl2psSetLastColor(rgba); - gl2psPrintf("%g %g %g C\n", rgba[0], rgba[1], rgba[2]); +static void gl2psPrintPostScriptColor(GL2PSrgba rgba) +{ + if (!gl2psSameColor(gl2ps->lastrgba, rgba)) + { + gl2psSetLastColor(rgba) ; + gl2psPrintf("%g %g %g C\n", rgba[0], rgba[1], rgba[2]) ; } } -static void gl2psResetPostScriptColor(void) { - gl2ps->lastrgba[0] = gl2ps->lastrgba[1] = gl2ps->lastrgba[2] = -1.; +static void gl2psResetPostScriptColor(void) +{ + gl2ps->lastrgba[0] = gl2ps->lastrgba[1] = gl2ps->lastrgba[2] = -1. ; } -static void gl2psEndPostScriptLine(void) { - int i; - if (gl2ps->lastvertex.rgba[0] >= 0.) { - gl2psPrintf("%g %g LE\n", gl2ps->lastvertex.xyz[0], - gl2ps->lastvertex.xyz[1]); - for (i = 0; i < 3; i++) - gl2ps->lastvertex.xyz[i] = -1.; - for (i = 0; i < 4; i++) - gl2ps->lastvertex.rgba[i] = -1.; +static void gl2psEndPostScriptLine(void) +{ + int i ; + if (gl2ps->lastvertex.rgba[0] >= 0.) + { + gl2psPrintf("%g %g LE\n", gl2ps->lastvertex.xyz[0], gl2ps->lastvertex.xyz[1]) ; + for (i = 0 ; i < 3; i++) + gl2ps->lastvertex.xyz[i] = -1. ; + for (i = 0 ; i < 4; i++) + gl2ps->lastvertex.rgba[i] = -1. ; } } -static void gl2psParseStipplePattern(GLushort pattern, GLint factor, int *nb, - int array[10]) { - int i, n; - int on[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; - int off[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; - char tmp[16]; +static void gl2psParseStipplePattern(GLushort pattern, GLint factor, int *nb, int array[10]) +{ + int i, n ; + int on[8] = { 0, 0, 0, 0, 0, 0, 0, 0 } ; + int off[8] = { 0, 0, 0, 0, 0, 0, 0, 0 } ; + char tmp[16] ; /* extract the 16 bits from the OpenGL stipple pattern */ - for (n = 15; n >= 0; n--) { - tmp[n] = (char) (pattern & 0x01); - pattern >>= 1; + for (n = 15 ; n >= 0; n--) + { + tmp[n] = (char)(pattern & 0x01) ; + pattern >>= 1 ; } /* compute the on/off pixel sequence */ - n = 0; - for (i = 0; i < 8; i++) { - while (n < 16 && !tmp[n]) { - off[i]++; - n++; + n = 0 ; + for (i = 0 ; i < 8; i++) + { + while (n < 16 && !tmp[n]) + { + off[i]++ ; + n++ ; } - while (n < 16 && tmp[n]) { - on[i]++; - n++; + while (n < 16 && tmp[n]) + { + on[i]++ ; + n++ ; } - if (n >= 15) { - i++; - break; + if (n >= 15) + { + i++ ; + break ; } } @@ -2929,252 +3192,263 @@ static void gl2psParseStipplePattern(GLushort pattern, GLint factor, int *nb, elements in the on/off array, so we limit ourselves to 5 on/off couples (our longest possible array is thus [on4 off4 on3 off3 on2 off2 on1 off1 on0 off0]) */ - *nb = 0; - for (n = i - 1; n >= 0; n--) { - array[(*nb)++] = factor * on[n]; - array[(*nb)++] = factor * off[n]; - if (*nb == 10) - break; + *nb = 0 ; + for (n = i - 1 ; n >= 0; n--) + { + array[(*nb)++] = factor * on[n] ; + array[(*nb)++] = factor * off[n] ; + if (*nb == 10) break ; } } -static int gl2psPrintPostScriptDash(GLushort pattern, GLint factor, - const char *str) { - int len = 0, i, n, array[10]; +static int gl2psPrintPostScriptDash(GLushort pattern, GLint factor, const char *str) +{ + int len = 0, i, n, array[10] ; - if (pattern == gl2ps->lastpattern && factor == gl2ps->lastfactor) - return 0; + if (pattern == gl2ps->lastpattern && factor == gl2ps->lastfactor) return 0 ; - gl2ps->lastpattern = pattern; - gl2ps->lastfactor = factor; + gl2ps->lastpattern = pattern ; + gl2ps->lastfactor = factor ; - if (!pattern || !factor) { + if (!pattern || !factor) + { /* solid line */ - len += gl2psPrintf("[] 0 %s\n", str); - } else { - gl2psParseStipplePattern(pattern, factor, &n, array); - len += gl2psPrintf("["); - for (i = 0; i < n; i++) { - if (i) - len += gl2psPrintf(" "); - len += gl2psPrintf("%d", array[i]); + len += gl2psPrintf("[] 0 %s\n", str) ; + } + else + { + gl2psParseStipplePattern(pattern, factor, &n, array) ; + len += gl2psPrintf("[") ; + for (i = 0 ; i < n; i++) + { + if (i) len += gl2psPrintf(" ") ; + len += gl2psPrintf("%d", array[i]) ; } - len += gl2psPrintf("] 0 %s\n", str); + len += gl2psPrintf("] 0 %s\n", str) ; } - return len; + return len ; } -static void gl2psPrintPostScriptPrimitive(void *data) { - int newline; - GL2PSprimitive *prim; +static void gl2psPrintPostScriptPrimitive(void *data) +{ + int newline ; + GL2PSprimitive *prim ; - prim = *(GL2PSprimitive**) data; + prim = *(GL2PSprimitive**)data ; - if ((gl2ps->options & GL2PS_OCCLUSION_CULL) && prim->culled) - return; + if ((gl2ps->options & GL2PS_OCCLUSION_CULL) && prim->culled) return ; /* Every effort is made to draw lines as connected segments (i.e., using a single PostScript path): this is the only way to get nice line joins and to not restart the stippling for every line segment. So if the primitive to print is not a line we must first finish the current line (if any): */ - if (prim->type != GL2PS_LINE) - gl2psEndPostScriptLine(); - - switch (prim->type) { - case GL2PS_POINT: - gl2psPrintPostScriptColor(prim->verts[0].rgba); - gl2psPrintf("%g %g %g P\n", prim->verts[0].xyz[0], - prim->verts[0].xyz[1], 0.5 * prim->width); - break; - case GL2PS_LINE: + if (prim->type != GL2PS_LINE) gl2psEndPostScriptLine() ; + + switch (prim->type) + { + case GL2PS_POINT : + gl2psPrintPostScriptColor(prim->verts[0].rgba) ; + gl2psPrintf("%g %g %g P\n", prim->verts[0].xyz[0], prim->verts[0].xyz[1], + 0.5 * prim->width) ; + break ; + case GL2PS_LINE : if (!gl2psSamePosition(gl2ps->lastvertex.xyz, prim->verts[0].xyz) - || !gl2psSameColor(gl2ps->lastrgba, prim->verts[0].rgba) - || gl2ps->lastlinewidth != prim->width - || gl2ps->lastpattern != prim->pattern - || gl2ps->lastfactor != prim->factor) { + || !gl2psSameColor(gl2ps->lastrgba, prim->verts[0].rgba) + || gl2ps->lastlinewidth != prim->width || gl2ps->lastpattern != prim->pattern + || gl2ps->lastfactor != prim->factor) + { /* End the current line if the new segment does not start where the last one ended, or if the color, the width or the stippling have changed (multi-stroking lines with changing colors is necessary until we use /shfill for lines; unfortunately this means that at the moment we can screw up line stippling for smooth-shaded lines) */ - gl2psEndPostScriptLine(); - newline = 1; - } else { - newline = 0; + gl2psEndPostScriptLine() ; + newline = 1 ; } - if (gl2ps->lastlinewidth != prim->width) { - gl2ps->lastlinewidth = prim->width; - gl2psPrintf("%g W\n", gl2ps->lastlinewidth); + else + { + newline = 0 ; + } + if (gl2ps->lastlinewidth != prim->width) + { + gl2ps->lastlinewidth = prim->width ; + gl2psPrintf("%g W\n", gl2ps->lastlinewidth) ; } - gl2psPrintPostScriptDash(prim->pattern, prim->factor, "setdash"); - gl2psPrintPostScriptColor(prim->verts[0].rgba); + gl2psPrintPostScriptDash(prim->pattern, prim->factor, "setdash") ; + gl2psPrintPostScriptColor(prim->verts[0].rgba) ; gl2psPrintf("%g %g %s\n", prim->verts[0].xyz[0], prim->verts[0].xyz[1], - newline ? "LS" : "L"); - gl2ps->lastvertex = prim->verts[1]; - break; - case GL2PS_TRIANGLE: - if (!gl2psVertsSameColor(prim)) { - gl2psResetPostScriptColor(); - gl2psPrintf("%g %g %g %g %g %g %g %g %g %g %g %g %g %g %g ST\n", - prim->verts[2].xyz[0], prim->verts[2].xyz[1], - prim->verts[2].rgba[0], prim->verts[2].rgba[1], - prim->verts[2].rgba[2], prim->verts[1].xyz[0], - prim->verts[1].xyz[1], prim->verts[1].rgba[0], - prim->verts[1].rgba[1], prim->verts[1].rgba[2], - prim->verts[0].xyz[0], prim->verts[0].xyz[1], - prim->verts[0].rgba[0], prim->verts[0].rgba[1], - prim->verts[0].rgba[2]); - } else { - gl2psPrintPostScriptColor(prim->verts[0].rgba); - gl2psPrintf("%g %g %g %g %g %g T\n", prim->verts[2].xyz[0], - prim->verts[2].xyz[1], prim->verts[1].xyz[0], - prim->verts[1].xyz[1], prim->verts[0].xyz[0], - prim->verts[0].xyz[1]); + newline ? "LS" : "L") ; + gl2ps->lastvertex = prim->verts[1] ; + break ; + case GL2PS_TRIANGLE : + if (!gl2psVertsSameColor(prim)) + { + gl2psResetPostScriptColor() ; + gl2psPrintf("%g %g %g %g %g %g %g %g %g %g %g %g %g %g %g ST\n", prim->verts[2].xyz[0], + prim->verts[2].xyz[1], prim->verts[2].rgba[0], prim->verts[2].rgba[1], + prim->verts[2].rgba[2], prim->verts[1].xyz[0], prim->verts[1].xyz[1], + prim->verts[1].rgba[0], prim->verts[1].rgba[1], prim->verts[1].rgba[2], + prim->verts[0].xyz[0], prim->verts[0].xyz[1], prim->verts[0].rgba[0], + prim->verts[0].rgba[1], prim->verts[0].rgba[2]) ; + } + else + { + gl2psPrintPostScriptColor(prim->verts[0].rgba) ; + gl2psPrintf("%g %g %g %g %g %g T\n", prim->verts[2].xyz[0], prim->verts[2].xyz[1], + prim->verts[1].xyz[0], prim->verts[1].xyz[1], prim->verts[0].xyz[0], + prim->verts[0].xyz[1]) ; } - break; - case GL2PS_QUADRANGLE: - gl2psMsg(GL2PS_WARNING, "There should not be any quad left to print"); - break; - case GL2PS_PIXMAP: - gl2psPrintPostScriptPixmap(prim->verts[0].xyz[0], prim->verts[0].xyz[1], - prim->data.image); - break; - case GL2PS_IMAGEMAP: - if (prim->data.image->type != GL2PS_IMAGEMAP_WRITTEN) { - gl2psPrintPostScriptColor(prim->verts[0].rgba); - gl2psPrintPostScriptImagemap(prim->data.image->pixels[0], - prim->data.image->pixels[1], prim->data.image->width, - prim->data.image->height, - (const unsigned char*) (&(prim->data.image->pixels[2]))); - prim->data.image->type = GL2PS_IMAGEMAP_WRITTEN; + break ; + case GL2PS_QUADRANGLE : + gl2psMsg(GL2PS_WARNING, "There should not be any quad left to print") ; + break ; + case GL2PS_PIXMAP : + gl2psPrintPostScriptPixmap(prim->verts[0].xyz[0], prim->verts[0].xyz[1], prim->data.image) ; + break ; + case GL2PS_IMAGEMAP : + if (prim->data.image->type != GL2PS_IMAGEMAP_WRITTEN) + { + gl2psPrintPostScriptColor(prim->verts[0].rgba) ; + gl2psPrintPostScriptImagemap(prim->data.image->pixels[0], prim->data.image->pixels[1], + prim->data.image->width, prim->data.image->height, + (const unsigned char*)(&(prim->data.image->pixels[2]))) ; + prim->data.image->type = GL2PS_IMAGEMAP_WRITTEN ; } - break; - case GL2PS_TEXT: - gl2psPrintPostScriptColor(prim->verts[0].rgba); - gl2psPrintf("(%s) ", prim->data.text->str); - if (prim->data.text->angle) - gl2psPrintf("%g ", prim->data.text->angle); - gl2psPrintf("%g %g %d /%s ", prim->verts[0].xyz[0], - prim->verts[0].xyz[1], prim->data.text->fontsize, - prim->data.text->fontname); - switch (prim->data.text->alignment) { - case GL2PS_TEXT_C: - gl2psPrintf(prim->data.text->angle ? "SCCR\n" : "SCC\n"); - break; - case GL2PS_TEXT_CL: - gl2psPrintf(prim->data.text->angle ? "SCLR\n" : "SCL\n"); - break; - case GL2PS_TEXT_CR: - gl2psPrintf(prim->data.text->angle ? "SCRR\n" : "SCR\n"); - break; - case GL2PS_TEXT_B: - gl2psPrintf(prim->data.text->angle ? "SBCR\n" : "SBC\n"); - break; - case GL2PS_TEXT_BR: - gl2psPrintf(prim->data.text->angle ? "SBRR\n" : "SBR\n"); - break; - case GL2PS_TEXT_T: - gl2psPrintf(prim->data.text->angle ? "STCR\n" : "STC\n"); - break; - case GL2PS_TEXT_TL: - gl2psPrintf(prim->data.text->angle ? "STLR\n" : "STL\n"); - break; - case GL2PS_TEXT_TR: - gl2psPrintf(prim->data.text->angle ? "STRR\n" : "STR\n"); - break; - case GL2PS_TEXT_BL: - default: - gl2psPrintf(prim->data.text->angle ? "SR\n" : "S\n"); - break; + break ; + case GL2PS_TEXT : + gl2psPrintPostScriptColor(prim->verts[0].rgba) ; + gl2psPrintf("(%s) ", prim->data.text->str) ; + if (prim->data.text->angle) gl2psPrintf("%g ", prim->data.text->angle) ; + gl2psPrintf("%g %g %d /%s ", prim->verts[0].xyz[0], prim->verts[0].xyz[1], + prim->data.text->fontsize, prim->data.text->fontname) ; + switch (prim->data.text->alignment) + { + case GL2PS_TEXT_C : + gl2psPrintf(prim->data.text->angle ? "SCCR\n" : "SCC\n") ; + break ; + case GL2PS_TEXT_CL : + gl2psPrintf(prim->data.text->angle ? "SCLR\n" : "SCL\n") ; + break ; + case GL2PS_TEXT_CR : + gl2psPrintf(prim->data.text->angle ? "SCRR\n" : "SCR\n") ; + break ; + case GL2PS_TEXT_B : + gl2psPrintf(prim->data.text->angle ? "SBCR\n" : "SBC\n") ; + break ; + case GL2PS_TEXT_BR : + gl2psPrintf(prim->data.text->angle ? "SBRR\n" : "SBR\n") ; + break ; + case GL2PS_TEXT_T : + gl2psPrintf(prim->data.text->angle ? "STCR\n" : "STC\n") ; + break ; + case GL2PS_TEXT_TL : + gl2psPrintf(prim->data.text->angle ? "STLR\n" : "STL\n") ; + break ; + case GL2PS_TEXT_TR : + gl2psPrintf(prim->data.text->angle ? "STRR\n" : "STR\n") ; + break ; + case GL2PS_TEXT_BL : + default : + gl2psPrintf(prim->data.text->angle ? "SR\n" : "S\n") ; + break ; } - break; - case GL2PS_SPECIAL: + break ; + case GL2PS_SPECIAL : /* alignment contains the format for which the special output text is intended */ - if (prim->data.text->alignment == GL2PS_PS - || prim->data.text->alignment == GL2PS_EPS) - gl2psPrintf("%s\n", prim->data.text->str); - break; - default: - break; + if (prim->data.text->alignment == GL2PS_PS || prim->data.text->alignment == GL2PS_EPS) gl2psPrintf( + "%s\n", prim->data.text->str) ; + break ; + default : + break ; } } -static void gl2psPrintPostScriptFooter(void) { +static void gl2psPrintPostScriptFooter(void) +{ gl2psPrintf("grestore\n" - "showpage\n" - "cleartomark\n" - "%%%%PageTrailer\n" - "%%%%Trailer\n" - "end\n" - "%%%%EOF\n"); + "showpage\n" + "cleartomark\n" + "%%%%PageTrailer\n" + "%%%%Trailer\n" + "end\n" + "%%%%EOF\n") ; - gl2psPrintGzipFooter(); + gl2psPrintGzipFooter() ; } -static void gl2psPrintPostScriptBeginViewport(GLint viewport[4]) { - GLint index; - GLfloat rgba[4]; - int x = viewport[0], y = viewport[1], w = viewport[2], h = viewport[3]; +static void gl2psPrintPostScriptBeginViewport(GLint viewport[4]) +{ + GLint index ; + GLfloat rgba[4] ; + int x = viewport[0], y = viewport[1], w = viewport[2], h = viewport[3] ; - glRenderMode(GL_FEEDBACK); + glRenderMode(GL_FEEDBACK) ; - if (gl2ps->header) { - gl2psPrintPostScriptHeader(); - gl2ps->header = GL_FALSE; + if (gl2ps->header) + { + gl2psPrintPostScriptHeader() ; + gl2ps->header = GL_FALSE ; } gl2psPrintf("gsave\n" - "1.0 1.0 scale\n"); - - if (gl2ps->options & GL2PS_DRAW_BACKGROUND) { - if (gl2ps->colormode == GL_RGBA || gl2ps->colorsize == 0) { - glGetFloatv(GL_COLOR_CLEAR_VALUE, rgba); - } else { - glGetIntegerv(GL_INDEX_CLEAR_VALUE, &index); - rgba[0] = gl2ps->colormap[index][0]; - rgba[1] = gl2ps->colormap[index][1]; - rgba[2] = gl2ps->colormap[index][2]; - rgba[3] = 1.0F; + "1.0 1.0 scale\n") ; + + if (gl2ps->options & GL2PS_DRAW_BACKGROUND) + { + if (gl2ps->colormode == GL_RGBA || gl2ps->colorsize == 0) + { + glGetFloatv(GL_COLOR_CLEAR_VALUE, rgba) ; + } + else + { + glGetIntegerv(GL_INDEX_CLEAR_VALUE, &index) ; + rgba[0] = gl2ps->colormap[index][0] ; + rgba[1] = gl2ps->colormap[index][1] ; + rgba[2] = gl2ps->colormap[index][2] ; + rgba[3] = 1.0F ; } gl2psPrintf("%g %g %g C\n" - "newpath %d %d moveto %d %d lineto %d %d lineto %d %d lineto\n" - "closepath fill\n", rgba[0], rgba[1], rgba[2], x, y, x + w, y, - x + w, y + h, x, y + h); + "newpath %d %d moveto %d %d lineto %d %d lineto %d %d lineto\n" + "closepath fill\n", + rgba[0], rgba[1], rgba[2], x, y, x + w, y, x + w, y + h, x, y + h) ; } gl2psPrintf("newpath %d %d moveto %d %d lineto %d %d lineto %d %d lineto\n" - "closepath clip\n", x, y, x + w, y, x + w, y + h, x, y + h); + "closepath clip\n", + x, y, x + w, y, x + w, y + h, x, y + h) ; } -static GLint gl2psPrintPostScriptEndViewport(void) { - GLint res; +static GLint gl2psPrintPostScriptEndViewport(void) +{ + GLint res ; - res = gl2psPrintPrimitives(); - gl2psPrintf("grestore\n"); - return res; + res = gl2psPrintPrimitives() ; + gl2psPrintf("grestore\n") ; + return res ; } -static void gl2psPrintPostScriptFinalPrimitive(void) { +static void gl2psPrintPostScriptFinalPrimitive(void) +{ /* End any remaining line, if any */ - gl2psEndPostScriptLine(); + gl2psEndPostScriptLine() ; } /* definition of the PostScript and Encapsulated PostScript backends */ -static GL2PSbackend gl2psPS = { gl2psPrintPostScriptHeader, - gl2psPrintPostScriptFooter, gl2psPrintPostScriptBeginViewport, - gl2psPrintPostScriptEndViewport, gl2psPrintPostScriptPrimitive, - gl2psPrintPostScriptFinalPrimitive, "ps", "Postscript" }; +static GL2PSbackend gl2psPS = { gl2psPrintPostScriptHeader, gl2psPrintPostScriptFooter, + gl2psPrintPostScriptBeginViewport, gl2psPrintPostScriptEndViewport, + gl2psPrintPostScriptPrimitive, gl2psPrintPostScriptFinalPrimitive, + "ps", "Postscript" } ; -static GL2PSbackend gl2psEPS = { gl2psPrintPostScriptHeader, - gl2psPrintPostScriptFooter, gl2psPrintPostScriptBeginViewport, - gl2psPrintPostScriptEndViewport, gl2psPrintPostScriptPrimitive, - gl2psPrintPostScriptFinalPrimitive, "eps", "Encapsulated Postscript" }; +static GL2PSbackend gl2psEPS = { gl2psPrintPostScriptHeader, gl2psPrintPostScriptFooter, + gl2psPrintPostScriptBeginViewport, gl2psPrintPostScriptEndViewport, + gl2psPrintPostScriptPrimitive, gl2psPrintPostScriptFinalPrimitive, + "eps", "Encapsulated Postscript" } ; /********************************************************************* * @@ -3182,131 +3456,140 @@ static GL2PSbackend gl2psEPS = { gl2psPrintPostScriptHeader, * *********************************************************************/ -static void gl2psPrintTeXHeader(void) { - char name[256]; - time_t now; - int i; - - if (gl2ps->filename && strlen(gl2ps->filename) < 256) { - for (i = strlen(gl2ps->filename) - 1; i >= 0; i--) { - if (gl2ps->filename[i] == '.') { - strncpy(name, gl2ps->filename, i); - name[i] = '\0'; - break; +static void gl2psPrintTeXHeader(void) +{ + char name[256] ; + time_t now ; + int i ; + + if (gl2ps->filename && strlen(gl2ps->filename) < 256) + { + for (i = strlen(gl2ps->filename) - 1 ; i >= 0; i--) + { + if (gl2ps->filename[i] == '.') + { + strncpy(name, gl2ps->filename, i) ; + name[i] = '\0' ; + break ; } } - if (i <= 0) - strcpy(name, gl2ps->filename); - } else { - strcpy(name, "untitled"); + if (i <= 0) strcpy(name, gl2ps->filename) ; + } + else + { + strcpy(name, "untitled") ; } - time(&now); + time(&now) ; fprintf(gl2ps->stream, "%% Title: %s\n" - "%% Creator: GL2PS %d.%d.%d%s, %s\n" - "%% For: %s\n" - "%% CreationDate: %s", gl2ps->title, GL2PS_MAJOR_VERSION, - GL2PS_MINOR_VERSION, GL2PS_PATCH_VERSION, GL2PS_EXTRA_VERSION, - GL2PS_COPYRIGHT, gl2ps->producer, ctime(&now)); + "%% Creator: GL2PS %d.%d.%d%s, %s\n" + "%% For: %s\n" + "%% CreationDate: %s", + gl2ps->title, GL2PS_MAJOR_VERSION, GL2PS_MINOR_VERSION, GL2PS_PATCH_VERSION, + GL2PS_EXTRA_VERSION, GL2PS_COPYRIGHT, gl2ps->producer, ctime(&now)) ; fprintf(gl2ps->stream, "\\setlength{\\unitlength}{1pt}\n" - "\\begin{picture}(0,0)\n" - "\\includegraphics{%s}\n" - "\\end{picture}%%\n" - "%s\\begin{picture}(%d,%d)(0,0)\n", name, - (gl2ps->options & GL2PS_LANDSCAPE) ? "\\rotatebox{90}{" : "", - (int) gl2ps->viewport[2], (int) gl2ps->viewport[3]); -} - -static void gl2psPrintTeXPrimitive(void *data) { - GL2PSprimitive *prim; - - prim = *(GL2PSprimitive**) data; - - switch (prim->type) { - case GL2PS_TEXT: - fprintf(gl2ps->stream, "\\fontsize{%d}{0}\n\\selectfont", - prim->data.text->fontsize); - fprintf(gl2ps->stream, "\\put(%g,%g){\\makebox(0,0)", - prim->verts[0].xyz[0], prim->verts[0].xyz[1]); - switch (prim->data.text->alignment) { - case GL2PS_TEXT_C: - fprintf(gl2ps->stream, "{"); - break; - case GL2PS_TEXT_CL: - fprintf(gl2ps->stream, "[l]{"); - break; - case GL2PS_TEXT_CR: - fprintf(gl2ps->stream, "[r]{"); - break; - case GL2PS_TEXT_B: - fprintf(gl2ps->stream, "[b]{"); - break; - case GL2PS_TEXT_BR: - fprintf(gl2ps->stream, "[br]{"); - break; - case GL2PS_TEXT_T: - fprintf(gl2ps->stream, "[t]{"); - break; - case GL2PS_TEXT_TL: - fprintf(gl2ps->stream, "[tl]{"); - break; - case GL2PS_TEXT_TR: - fprintf(gl2ps->stream, "[tr]{"); - break; - case GL2PS_TEXT_BL: - default: - fprintf(gl2ps->stream, "[bl]{"); - break; + "\\begin{picture}(0,0)\n" + "\\includegraphics{%s}\n" + "\\end{picture}%%\n" + "%s\\begin{picture}(%d,%d)(0,0)\n", + name, (gl2ps->options & GL2PS_LANDSCAPE) ? "\\rotatebox{90}{" : "", + (int)gl2ps->viewport[2], (int)gl2ps->viewport[3]) ; +} + +static void gl2psPrintTeXPrimitive(void *data) +{ + GL2PSprimitive *prim ; + + prim = *(GL2PSprimitive**)data ; + + switch (prim->type) + { + case GL2PS_TEXT : + fprintf(gl2ps->stream, "\\fontsize{%d}{0}\n\\selectfont", prim->data.text->fontsize) ; + fprintf(gl2ps->stream, "\\put(%g,%g){\\makebox(0,0)", prim->verts[0].xyz[0], + prim->verts[0].xyz[1]) ; + switch (prim->data.text->alignment) + { + case GL2PS_TEXT_C : + fprintf(gl2ps->stream, "{") ; + break ; + case GL2PS_TEXT_CL : + fprintf(gl2ps->stream, "[l]{") ; + break ; + case GL2PS_TEXT_CR : + fprintf(gl2ps->stream, "[r]{") ; + break ; + case GL2PS_TEXT_B : + fprintf(gl2ps->stream, "[b]{") ; + break ; + case GL2PS_TEXT_BR : + fprintf(gl2ps->stream, "[br]{") ; + break ; + case GL2PS_TEXT_T : + fprintf(gl2ps->stream, "[t]{") ; + break ; + case GL2PS_TEXT_TL : + fprintf(gl2ps->stream, "[tl]{") ; + break ; + case GL2PS_TEXT_TR : + fprintf(gl2ps->stream, "[tr]{") ; + break ; + case GL2PS_TEXT_BL : + default : + fprintf(gl2ps->stream, "[bl]{") ; + break ; } - if (prim->data.text->angle) - fprintf(gl2ps->stream, "\\rotatebox{%g}{", prim->data.text->angle); - fprintf(gl2ps->stream, "\\textcolor[rgb]{%g,%g,%g}{{%s}}", - prim->verts[0].rgba[0], prim->verts[0].rgba[1], - prim->verts[0].rgba[2], prim->data.text->str); - if (prim->data.text->angle) - fprintf(gl2ps->stream, "}"); - fprintf(gl2ps->stream, "}}\n"); - break; - case GL2PS_SPECIAL: + if (prim->data.text->angle) fprintf(gl2ps->stream, "\\rotatebox{%g}{", + prim->data.text->angle) ; + fprintf(gl2ps->stream, "\\textcolor[rgb]{%g,%g,%g}{{%s}}", prim->verts[0].rgba[0], + prim->verts[0].rgba[1], prim->verts[0].rgba[2], prim->data.text->str) ; + if (prim->data.text->angle) fprintf(gl2ps->stream, "}") ; + fprintf(gl2ps->stream, "}}\n") ; + break ; + case GL2PS_SPECIAL : /* alignment contains the format for which the special output text is intended */ - if (prim->data.text->alignment == GL2PS_TEX) - fprintf(gl2ps->stream, "%s\n", prim->data.text->str); - break; - default: - break; + if (prim->data.text->alignment == GL2PS_TEX) fprintf(gl2ps->stream, "%s\n", + prim->data.text->str) ; + break ; + default : + break ; } } -static void gl2psPrintTeXFooter(void) { - fprintf(gl2ps->stream, "\\end{picture}%s\n", - (gl2ps->options & GL2PS_LANDSCAPE) ? "}" : ""); +static void gl2psPrintTeXFooter(void) +{ + fprintf(gl2ps->stream, "\\end{picture}%s\n", (gl2ps->options & GL2PS_LANDSCAPE) ? "}" : "") ; } -static void gl2psPrintTeXBeginViewport(GLint viewport[4]) { - glRenderMode(GL_FEEDBACK); +static void gl2psPrintTeXBeginViewport(GLint viewport[4]) +{ + glRenderMode(GL_FEEDBACK) ; - if (gl2ps->header) { - gl2psPrintTeXHeader(); - gl2ps->header = GL_FALSE; + if (gl2ps->header) + { + gl2psPrintTeXHeader() ; + gl2ps->header = GL_FALSE ; } } -static GLint gl2psPrintTeXEndViewport(void) { - return gl2psPrintPrimitives(); +static GLint gl2psPrintTeXEndViewport(void) +{ + return gl2psPrintPrimitives() ; } -static void gl2psPrintTeXFinalPrimitive(void) { +static void gl2psPrintTeXFinalPrimitive(void) +{ } /* definition of the LaTeX backend */ -static GL2PSbackend gl2psTEX = - { gl2psPrintTeXHeader, gl2psPrintTeXFooter, gl2psPrintTeXBeginViewport, - gl2psPrintTeXEndViewport, gl2psPrintTeXPrimitive, - gl2psPrintTeXFinalPrimitive, "tex", "LaTeX text" }; +static GL2PSbackend gl2psTEX = { gl2psPrintTeXHeader, gl2psPrintTeXFooter, + gl2psPrintTeXBeginViewport, gl2psPrintTeXEndViewport, + gl2psPrintTeXPrimitive, gl2psPrintTeXFinalPrimitive, "tex", + "LaTeX text" } ; /********************************************************************* * @@ -3314,906 +3597,969 @@ static GL2PSbackend gl2psTEX = * *********************************************************************/ -static int gl2psPrintPDFCompressorType(void) { +static int gl2psPrintPDFCompressorType(void) +{ #if defined(GL2PS_HAVE_ZLIB) - if(gl2ps->options & GL2PS_COMPRESS) { - return fprintf(gl2ps->stream, "/Filter [/FlateDecode]\n"); + if(gl2ps->options & GL2PS_COMPRESS) + { + return fprintf(gl2ps->stream, "/Filter [/FlateDecode]\n") ; } #endif - return 0; + return 0 ; } -static int gl2psPrintPDFStrokeColor(GL2PSrgba rgba) { - int i, offs = 0; +static int gl2psPrintPDFStrokeColor(GL2PSrgba rgba) +{ + int i, offs = 0 ; - gl2psSetLastColor(rgba); - for (i = 0; i < 3; ++i) { + gl2psSetLastColor(rgba) ; + for (i = 0 ; i < 3; ++i) + { if (GL2PS_ZERO(rgba[i])) - offs += gl2psPrintf("%.0f ", 0.); + offs += gl2psPrintf("%.0f ", 0.) ; else if (rgba[i] < 1e-4 || rgba[i] > 1e6) /* avoid %e formatting */ - offs += gl2psPrintf("%f ", rgba[i]); + offs += gl2psPrintf("%f ", rgba[i]) ; else - offs += gl2psPrintf("%g ", rgba[i]); + offs += gl2psPrintf("%g ", rgba[i]) ; } - offs += gl2psPrintf("RG\n"); - return offs; + offs += gl2psPrintf("RG\n") ; + return offs ; } -static int gl2psPrintPDFFillColor(GL2PSrgba rgba) { - int i, offs = 0; +static int gl2psPrintPDFFillColor(GL2PSrgba rgba) +{ + int i, offs = 0 ; - for (i = 0; i < 3; ++i) { + for (i = 0 ; i < 3; ++i) + { if (GL2PS_ZERO(rgba[i])) - offs += gl2psPrintf("%.0f ", 0.); + offs += gl2psPrintf("%.0f ", 0.) ; else if (rgba[i] < 1e-4 || rgba[i] > 1e6) /* avoid %e formatting */ - offs += gl2psPrintf("%f ", rgba[i]); + offs += gl2psPrintf("%f ", rgba[i]) ; else - offs += gl2psPrintf("%g ", rgba[i]); + offs += gl2psPrintf("%g ", rgba[i]) ; } - offs += gl2psPrintf("rg\n"); - return offs; + offs += gl2psPrintf("rg\n") ; + return offs ; } -static int gl2psPrintPDFLineWidth(GLfloat lw) { +static int gl2psPrintPDFLineWidth(GLfloat lw) +{ if (GL2PS_ZERO(lw)) - return gl2psPrintf("%.0f w\n", 0.); + return gl2psPrintf("%.0f w\n", 0.) ; else if (lw < 1e-4 || lw > 1e6) /* avoid %e formatting */ - return gl2psPrintf("%f w\n", lw); + return gl2psPrintf("%f w\n", lw) ; else - return gl2psPrintf("%g w\n", lw); + return gl2psPrintf("%g w\n", lw) ; } -static void gl2psPutPDFText(GL2PSstring *text, int cnt, GLfloat x, GLfloat y) { - GLfloat rad, crad, srad; +static void gl2psPutPDFText(GL2PSstring *text, int cnt, GLfloat x, GLfloat y) +{ + GLfloat rad, crad, srad ; - if (text->angle == 0.0F) { + if (text->angle == 0.0F) + { gl2ps->streamlength += gl2psPrintf("BT\n" - "/F%d %d Tf\n" - "%f %f Td\n" - "(%s) Tj\n" - "ET\n", cnt, text->fontsize, x, y, text->str); - } else { - rad = M_PI * text->angle / 180.0F; - srad = (GLfloat) sin(rad); - crad = (GLfloat) cos(rad); + "/F%d %d Tf\n" + "%f %f Td\n" + "(%s) Tj\n" + "ET\n", + cnt, text->fontsize, x, y, text->str) ; + } + else + { + rad = M_PI * text->angle / 180.0F ; + srad = (GLfloat)sin(rad) ; + crad = (GLfloat)cos(rad) ; gl2ps->streamlength += gl2psPrintf("BT\n" - "/F%d %d Tf\n" - "%f %f %f %f %f %f Tm\n" - "(%s) Tj\n" - "ET\n", cnt, text->fontsize, crad, srad, -srad, crad, x, y, - text->str); + "/F%d %d Tf\n" + "%f %f %f %f %f %f Tm\n" + "(%s) Tj\n" + "ET\n", + cnt, text->fontsize, crad, srad, -srad, crad, x, y, + text->str) ; } } -static void gl2psPutPDFImage(GL2PSimage *image, int cnt, GLfloat x, GLfloat y) { +static void gl2psPutPDFImage(GL2PSimage *image, int cnt, GLfloat x, GLfloat y) +{ gl2ps->streamlength += gl2psPrintf("q\n" - "%d 0 0 %d %f %f cm\n" - "/Im%d Do\n" - "Q\n", (int) image->width, (int) image->height, x, y, cnt); + "%d 0 0 %d %f %f cm\n" + "/Im%d Do\n" + "Q\n", + (int)image->width, (int)image->height, x, y, cnt) ; } -static void gl2psPDFstacksInit(void) { - gl2ps->objects_stack = 7 /* FIXED_XREF_ENTRIES */+ 1; - gl2ps->extgs_stack = 0; - gl2ps->font_stack = 0; - gl2ps->im_stack = 0; - gl2ps->trgroupobjects_stack = 0; - gl2ps->shader_stack = 0; - gl2ps->mshader_stack = 0; +static void gl2psPDFstacksInit(void) +{ + gl2ps->objects_stack = 7 /* FIXED_XREF_ENTRIES */+ 1 ; + gl2ps->extgs_stack = 0 ; + gl2ps->font_stack = 0 ; + gl2ps->im_stack = 0 ; + gl2ps->trgroupobjects_stack = 0 ; + gl2ps->shader_stack = 0 ; + gl2ps->mshader_stack = 0 ; } -static void gl2psPDFgroupObjectInit(GL2PSpdfgroup *gro) { - if (!gro) - return; +static void gl2psPDFgroupObjectInit(GL2PSpdfgroup *gro) +{ + if (!gro) return ; - gro->ptrlist = NULL; - gro->fontno = gro->gsno = gro->imno = gro->maskshno = gro->shno = - gro->trgroupno = gro->fontobjno = gro->imobjno = gro->shobjno = - gro->maskshobjno = gro->gsobjno = gro->trgroupobjno = -1; + gro->ptrlist = NULL ; + gro->fontno = gro->gsno = gro->imno = gro->maskshno = gro->shno = gro->trgroupno = gro->fontobjno = gro->imobjno = gro->shobjno = gro->maskshobjno = gro->gsobjno = gro->trgroupobjno = -1 ; } /* Build up group objects and assign name and object numbers */ -static void gl2psPDFgroupListInit(void) { - int i; - GL2PSprimitive *p = NULL; - GL2PSpdfgroup gro; - int lasttype = GL2PS_NO_TYPE; - GL2PSrgba lastrgba = { -1.0F, -1.0F, -1.0F, -1.0F }; - GLushort lastpattern = 0; - GLint lastfactor = 0; - GLfloat lastwidth = 1; - GL2PStriangle lastt, tmpt; - int lastTriangleWasNotSimpleWithSameColor = 0; - - if (!gl2ps->pdfprimlist) - return; - - gl2ps->pdfgrouplist = gl2psListCreate(500, 500, sizeof(GL2PSpdfgroup)); - gl2psInitTriangle(&lastt); - - for (i = 0; i < gl2psListNbr(gl2ps->pdfprimlist); ++i) { - p = *(GL2PSprimitive**) gl2psListPointer(gl2ps->pdfprimlist, i); - switch (p->type) { - case GL2PS_PIXMAP: - gl2psPDFgroupObjectInit(&gro); - gro.ptrlist = gl2psListCreate(1, 2, sizeof(GL2PSprimitive*)); - gro.imno = gl2ps->im_stack++; - gl2psListAdd(gro.ptrlist, &p); - gl2psListAdd(gl2ps->pdfgrouplist, &gro); - break; - case GL2PS_TEXT: - gl2psPDFgroupObjectInit(&gro); - gro.ptrlist = gl2psListCreate(1, 2, sizeof(GL2PSprimitive*)); - gro.fontno = gl2ps->font_stack++; - gl2psListAdd(gro.ptrlist, &p); - gl2psListAdd(gl2ps->pdfgrouplist, &gro); - break; - case GL2PS_LINE: - if (lasttype != p->type || lastwidth != p->width - || lastpattern != p->pattern || lastfactor != p->factor - || !gl2psSameColor(p->verts[0].rgba, lastrgba)) { - gl2psPDFgroupObjectInit(&gro); - gro.ptrlist = gl2psListCreate(1, 2, sizeof(GL2PSprimitive*)); - gl2psListAdd(gro.ptrlist, &p); - gl2psListAdd(gl2ps->pdfgrouplist, &gro); - } else { - gl2psListAdd(gro.ptrlist, &p); +static void gl2psPDFgroupListInit(void) +{ + int i ; + GL2PSprimitive *p = NULL ; + GL2PSpdfgroup gro ; + int lasttype = GL2PS_NO_TYPE ; + GL2PSrgba lastrgba = { -1.0F, -1.0F, -1.0F, -1.0F } ; + GLushort lastpattern = 0 ; + GLint lastfactor = 0 ; + GLfloat lastwidth = 1 ; + GL2PStriangle lastt, tmpt ; + int lastTriangleWasNotSimpleWithSameColor = 0 ; + + if (!gl2ps->pdfprimlist) return ; + + gl2ps->pdfgrouplist = gl2psListCreate(500, 500, sizeof(GL2PSpdfgroup)) ; + gl2psInitTriangle(&lastt) ; + + for (i = 0 ; i < gl2psListNbr(gl2ps->pdfprimlist); ++i) + { + p = *(GL2PSprimitive**)gl2psListPointer(gl2ps->pdfprimlist, i) ; + switch (p->type) + { + case GL2PS_PIXMAP : + gl2psPDFgroupObjectInit(&gro) ; + gro.ptrlist = gl2psListCreate(1, 2, sizeof(GL2PSprimitive*)) ; + gro.imno = gl2ps->im_stack++ ; + gl2psListAdd(gro.ptrlist, &p) ; + gl2psListAdd(gl2ps->pdfgrouplist, &gro) ; + break ; + case GL2PS_TEXT : + gl2psPDFgroupObjectInit(&gro) ; + gro.ptrlist = gl2psListCreate(1, 2, sizeof(GL2PSprimitive*)) ; + gro.fontno = gl2ps->font_stack++ ; + gl2psListAdd(gro.ptrlist, &p) ; + gl2psListAdd(gl2ps->pdfgrouplist, &gro) ; + break ; + case GL2PS_LINE : + if (lasttype != p->type || lastwidth != p->width || lastpattern != p->pattern + || lastfactor != p->factor || !gl2psSameColor(p->verts[0].rgba, lastrgba)) + { + gl2psPDFgroupObjectInit(&gro) ; + gro.ptrlist = gl2psListCreate(1, 2, sizeof(GL2PSprimitive*)) ; + gl2psListAdd(gro.ptrlist, &p) ; + gl2psListAdd(gl2ps->pdfgrouplist, &gro) ; } - lastpattern = p->pattern; - lastfactor = p->factor; - lastwidth = p->width; - lastrgba[0] = p->verts[0].rgba[0]; - lastrgba[1] = p->verts[0].rgba[1]; - lastrgba[2] = p->verts[0].rgba[2]; - break; - case GL2PS_POINT: + else + { + gl2psListAdd(gro.ptrlist, &p) ; + } + lastpattern = p->pattern ; + lastfactor = p->factor ; + lastwidth = p->width ; + lastrgba[0] = p->verts[0].rgba[0] ; + lastrgba[1] = p->verts[0].rgba[1] ; + lastrgba[2] = p->verts[0].rgba[2] ; + break ; + case GL2PS_POINT : if (lasttype != p->type || lastwidth != p->width - || !gl2psSameColor(p->verts[0].rgba, lastrgba)) { - gl2psPDFgroupObjectInit(&gro); - gro.ptrlist = gl2psListCreate(1, 2, sizeof(GL2PSprimitive*)); - gl2psListAdd(gro.ptrlist, &p); - gl2psListAdd(gl2ps->pdfgrouplist, &gro); - } else { - gl2psListAdd(gro.ptrlist, &p); + || !gl2psSameColor(p->verts[0].rgba, lastrgba)) + { + gl2psPDFgroupObjectInit(&gro) ; + gro.ptrlist = gl2psListCreate(1, 2, sizeof(GL2PSprimitive*)) ; + gl2psListAdd(gro.ptrlist, &p) ; + gl2psListAdd(gl2ps->pdfgrouplist, &gro) ; } - lastwidth = p->width; - lastrgba[0] = p->verts[0].rgba[0]; - lastrgba[1] = p->verts[0].rgba[1]; - lastrgba[2] = p->verts[0].rgba[2]; - break; - case GL2PS_TRIANGLE: - gl2psFillTriangleFromPrimitive(&tmpt, p, GL_TRUE); + else + { + gl2psListAdd(gro.ptrlist, &p) ; + } + lastwidth = p->width ; + lastrgba[0] = p->verts[0].rgba[0] ; + lastrgba[1] = p->verts[0].rgba[1] ; + lastrgba[2] = p->verts[0].rgba[2] ; + break ; + case GL2PS_TRIANGLE : + gl2psFillTriangleFromPrimitive(&tmpt, p, GL_TRUE) ; lastTriangleWasNotSimpleWithSameColor = !(tmpt.prop & T_CONST_COLOR - && tmpt.prop & T_ALPHA_1) - || !gl2psSameColor(tmpt.vertex[0].rgba, - lastt.vertex[0].rgba); + && tmpt.prop & T_ALPHA_1) + || !gl2psSameColor(tmpt.vertex[0].rgba, lastt.vertex[0].rgba) ; if (lasttype == p->type && tmpt.prop == lastt.prop - && lastTriangleWasNotSimpleWithSameColor) { + && lastTriangleWasNotSimpleWithSameColor) + { /* TODO Check here for last alpha */ - gl2psListAdd(gro.ptrlist, &p); - } else { - gl2psPDFgroupObjectInit(&gro); - gro.ptrlist = gl2psListCreate(1, 2, sizeof(GL2PSprimitive*)); - gl2psListAdd(gro.ptrlist, &p); - gl2psListAdd(gl2ps->pdfgrouplist, &gro); + gl2psListAdd(gro.ptrlist, &p) ; + } + else + { + gl2psPDFgroupObjectInit(&gro) ; + gro.ptrlist = gl2psListCreate(1, 2, sizeof(GL2PSprimitive*)) ; + gl2psListAdd(gro.ptrlist, &p) ; + gl2psListAdd(gl2ps->pdfgrouplist, &gro) ; } - lastt = tmpt; - break; - default: - break; + lastt = tmpt ; + break ; + default : + break ; } - lasttype = p->type; + lasttype = p->type ; } } -static void gl2psSortOutTrianglePDFgroup(GL2PSpdfgroup *gro) { - GL2PStriangle t; - GL2PSprimitive *prim = NULL; +static void gl2psSortOutTrianglePDFgroup(GL2PSpdfgroup *gro) +{ + GL2PStriangle t ; + GL2PSprimitive *prim = NULL ; - if (!gro) - return; + if (!gro) return ; - if (!gl2psListNbr(gro->ptrlist)) - return; + if (!gl2psListNbr(gro->ptrlist)) return ; - prim = *(GL2PSprimitive**) gl2psListPointer(gro->ptrlist, 0); + prim = *(GL2PSprimitive**)gl2psListPointer(gro->ptrlist, 0) ; - if (prim->type != GL2PS_TRIANGLE) - return; + if (prim->type != GL2PS_TRIANGLE) return ; - gl2psFillTriangleFromPrimitive(&t, prim, GL_TRUE); + gl2psFillTriangleFromPrimitive(&t, prim, GL_TRUE) ; - if (t.prop & T_CONST_COLOR && t.prop & T_ALPHA_LESS_1) { - gro->gsno = gl2ps->extgs_stack++; - gro->gsobjno = gl2ps->objects_stack++; - } else if (t.prop & T_CONST_COLOR && t.prop & T_VAR_ALPHA) { - gro->gsno = gl2ps->extgs_stack++; - gro->gsobjno = gl2ps->objects_stack++; - gro->trgroupno = gl2ps->trgroupobjects_stack++; - gro->trgroupobjno = gl2ps->objects_stack++; - gro->maskshno = gl2ps->mshader_stack++; - gro->maskshobjno = gl2ps->objects_stack++; - } else if (t.prop & T_VAR_COLOR && t.prop & T_ALPHA_1) { - gro->shno = gl2ps->shader_stack++; - gro->shobjno = gl2ps->objects_stack++; - } else if (t.prop & T_VAR_COLOR && t.prop & T_ALPHA_LESS_1) { - gro->gsno = gl2ps->extgs_stack++; - gro->gsobjno = gl2ps->objects_stack++; - gro->shno = gl2ps->shader_stack++; - gro->shobjno = gl2ps->objects_stack++; - } else if (t.prop & T_VAR_COLOR && t.prop & T_VAR_ALPHA) { - gro->gsno = gl2ps->extgs_stack++; - gro->gsobjno = gl2ps->objects_stack++; - gro->shno = gl2ps->shader_stack++; - gro->shobjno = gl2ps->objects_stack++; - gro->trgroupno = gl2ps->trgroupobjects_stack++; - gro->trgroupobjno = gl2ps->objects_stack++; - gro->maskshno = gl2ps->mshader_stack++; - gro->maskshobjno = gl2ps->objects_stack++; + if (t.prop & T_CONST_COLOR && t.prop & T_ALPHA_LESS_1) + { + gro->gsno = gl2ps->extgs_stack++ ; + gro->gsobjno = gl2ps->objects_stack++ ; + } + else if (t.prop & T_CONST_COLOR && t.prop & T_VAR_ALPHA) + { + gro->gsno = gl2ps->extgs_stack++ ; + gro->gsobjno = gl2ps->objects_stack++ ; + gro->trgroupno = gl2ps->trgroupobjects_stack++ ; + gro->trgroupobjno = gl2ps->objects_stack++ ; + gro->maskshno = gl2ps->mshader_stack++ ; + gro->maskshobjno = gl2ps->objects_stack++ ; + } + else if (t.prop & T_VAR_COLOR && t.prop & T_ALPHA_1) + { + gro->shno = gl2ps->shader_stack++ ; + gro->shobjno = gl2ps->objects_stack++ ; + } + else if (t.prop & T_VAR_COLOR && t.prop & T_ALPHA_LESS_1) + { + gro->gsno = gl2ps->extgs_stack++ ; + gro->gsobjno = gl2ps->objects_stack++ ; + gro->shno = gl2ps->shader_stack++ ; + gro->shobjno = gl2ps->objects_stack++ ; + } + else if (t.prop & T_VAR_COLOR && t.prop & T_VAR_ALPHA) + { + gro->gsno = gl2ps->extgs_stack++ ; + gro->gsobjno = gl2ps->objects_stack++ ; + gro->shno = gl2ps->shader_stack++ ; + gro->shobjno = gl2ps->objects_stack++ ; + gro->trgroupno = gl2ps->trgroupobjects_stack++ ; + gro->trgroupobjno = gl2ps->objects_stack++ ; + gro->maskshno = gl2ps->mshader_stack++ ; + gro->maskshobjno = gl2ps->objects_stack++ ; } } /* Main stream data */ -static void gl2psPDFgroupListWriteMainStream(void) { - int i, j, lastel; - GL2PSprimitive *prim = NULL, *prev = NULL; - GL2PSpdfgroup *gro; - GL2PStriangle t; - - if (!gl2ps->pdfgrouplist) - return; - - for (i = 0; i < gl2psListNbr(gl2ps->pdfgrouplist); ++i) { - gro = (GL2PSpdfgroup*) gl2psListPointer(gl2ps->pdfgrouplist, i); - - lastel = gl2psListNbr(gro->ptrlist) - 1; - if (lastel < 0) - continue; - - prim = *(GL2PSprimitive**) gl2psListPointer(gro->ptrlist, 0); - - switch (prim->type) { - case GL2PS_POINT: - gl2ps->streamlength += gl2psPrintf("1 J\n"); - gl2ps->streamlength += gl2psPrintPDFLineWidth(prim->width); - gl2ps->streamlength += gl2psPrintPDFStrokeColor( - prim->verts[0].rgba); - for (j = 0; j <= lastel; ++j) { - prim = *(GL2PSprimitive**) gl2psListPointer(gro->ptrlist, j); - gl2ps->streamlength += gl2psPrintf("%f %f m %f %f l\n", - prim->verts[0].xyz[0], prim->verts[0].xyz[1], - prim->verts[0].xyz[0], prim->verts[0].xyz[1]); +static void gl2psPDFgroupListWriteMainStream(void) +{ + int i, j, lastel ; + GL2PSprimitive *prim = NULL, *prev = NULL ; + GL2PSpdfgroup *gro ; + GL2PStriangle t ; + + if (!gl2ps->pdfgrouplist) return ; + + for (i = 0 ; i < gl2psListNbr(gl2ps->pdfgrouplist); ++i) + { + gro = (GL2PSpdfgroup*)gl2psListPointer(gl2ps->pdfgrouplist, i) ; + + lastel = gl2psListNbr(gro->ptrlist) - 1 ; + if (lastel < 0) continue ; + + prim = *(GL2PSprimitive**)gl2psListPointer(gro->ptrlist, 0) ; + + switch (prim->type) + { + case GL2PS_POINT : + gl2ps->streamlength += gl2psPrintf("1 J\n") ; + gl2ps->streamlength += gl2psPrintPDFLineWidth(prim->width) ; + gl2ps->streamlength += gl2psPrintPDFStrokeColor(prim->verts[0].rgba) ; + for (j = 0 ; j <= lastel; ++j) + { + prim = *(GL2PSprimitive**)gl2psListPointer(gro->ptrlist, j) ; + gl2ps->streamlength += gl2psPrintf("%f %f m %f %f l\n", prim->verts[0].xyz[0], + prim->verts[0].xyz[1], prim->verts[0].xyz[0], + prim->verts[0].xyz[1]) ; } - gl2ps->streamlength += gl2psPrintf("S\n"); - gl2ps->streamlength += gl2psPrintf("0 J\n"); - break; - case GL2PS_LINE: + gl2ps->streamlength += gl2psPrintf("S\n") ; + gl2ps->streamlength += gl2psPrintf("0 J\n") ; + break ; + case GL2PS_LINE : /* We try to use as few paths as possible to draw lines, in order to get nice stippling even when the individual segments are smaller than the stipple */ - gl2ps->streamlength += gl2psPrintPDFLineWidth(prim->width); - gl2ps->streamlength += gl2psPrintPDFStrokeColor( - prim->verts[0].rgba); - gl2ps->streamlength += gl2psPrintPostScriptDash(prim->pattern, - prim->factor, "d"); + gl2ps->streamlength += gl2psPrintPDFLineWidth(prim->width) ; + gl2ps->streamlength += gl2psPrintPDFStrokeColor(prim->verts[0].rgba) ; + gl2ps->streamlength += gl2psPrintPostScriptDash(prim->pattern, prim->factor, "d") ; /* start new path */ - gl2ps->streamlength += gl2psPrintf("%f %f m\n", - prim->verts[0].xyz[0], prim->verts[0].xyz[1]); - - for (j = 1; j <= lastel; ++j) { - prev = prim; - prim = *(GL2PSprimitive**) gl2psListPointer(gro->ptrlist, j); - if (!gl2psSamePosition(prim->verts[0].xyz, - prev->verts[1].xyz)) { + gl2ps->streamlength += gl2psPrintf("%f %f m\n", prim->verts[0].xyz[0], + prim->verts[0].xyz[1]) ; + + for (j = 1 ; j <= lastel; ++j) + { + prev = prim ; + prim = *(GL2PSprimitive**)gl2psListPointer(gro->ptrlist, j) ; + if (!gl2psSamePosition(prim->verts[0].xyz, prev->verts[1].xyz)) + { /* the starting point of the new segment does not match the end point of the previous line, so we end the current path and start a new one */ - gl2ps->streamlength += gl2psPrintf("%f %f l\n", - prev->verts[1].xyz[0], prev->verts[1].xyz[1]); - gl2ps->streamlength += gl2psPrintf("%f %f m\n", - prim->verts[0].xyz[0], prim->verts[0].xyz[1]); - } else { + gl2ps->streamlength += gl2psPrintf("%f %f l\n", prev->verts[1].xyz[0], + prev->verts[1].xyz[1]) ; + gl2ps->streamlength += gl2psPrintf("%f %f m\n", prim->verts[0].xyz[0], + prim->verts[0].xyz[1]) ; + } + else + { /* the two segements are connected, so we just append to the current path */ - gl2ps->streamlength += gl2psPrintf("%f %f l\n", - prim->verts[0].xyz[0], prim->verts[0].xyz[1]); + gl2ps->streamlength += gl2psPrintf("%f %f l\n", prim->verts[0].xyz[0], + prim->verts[0].xyz[1]) ; } } /* end last path */ - gl2ps->streamlength += gl2psPrintf("%f %f l\n", - prim->verts[1].xyz[0], prim->verts[1].xyz[1]); - gl2ps->streamlength += gl2psPrintf("S\n"); - break; - case GL2PS_TRIANGLE: - gl2psFillTriangleFromPrimitive(&t, prim, GL_TRUE); - gl2psSortOutTrianglePDFgroup(gro); + gl2ps->streamlength += gl2psPrintf("%f %f l\n", prim->verts[1].xyz[0], + prim->verts[1].xyz[1]) ; + gl2ps->streamlength += gl2psPrintf("S\n") ; + break ; + case GL2PS_TRIANGLE : + gl2psFillTriangleFromPrimitive(&t, prim, GL_TRUE) ; + gl2psSortOutTrianglePDFgroup(gro) ; /* No alpha and const color: Simple PDF draw orders */ - if (t.prop & T_CONST_COLOR && t.prop & T_ALPHA_1) { - gl2ps->streamlength += gl2psPrintPDFFillColor(t.vertex[0].rgba); - for (j = 0; j <= lastel; ++j) { - prim = *(GL2PSprimitive**) gl2psListPointer(gro->ptrlist, - j); - gl2psFillTriangleFromPrimitive(&t, prim, GL_FALSE); + if (t.prop & T_CONST_COLOR && t.prop & T_ALPHA_1) + { + gl2ps->streamlength += gl2psPrintPDFFillColor(t.vertex[0].rgba) ; + for (j = 0 ; j <= lastel; ++j) + { + prim = *(GL2PSprimitive**)gl2psListPointer(gro->ptrlist, j) ; + gl2psFillTriangleFromPrimitive(&t, prim, GL_FALSE) ; gl2ps->streamlength += gl2psPrintf("%f %f m\n" - "%f %f l\n" - "%f %f l\n" - "h f\n", t.vertex[0].xyz[0], t.vertex[0].xyz[1], - t.vertex[1].xyz[0], t.vertex[1].xyz[1], - t.vertex[2].xyz[0], t.vertex[2].xyz[1]); + "%f %f l\n" + "%f %f l\n" + "h f\n", + t.vertex[0].xyz[0], t.vertex[0].xyz[1], + t.vertex[1].xyz[0], t.vertex[1].xyz[1], + t.vertex[2].xyz[0], t.vertex[2].xyz[1]) ; } } /* Const alpha < 1 and const color: Simple PDF draw orders and an extra extended Graphics State for the alpha const */ - else if (t.prop & T_CONST_COLOR && t.prop & T_ALPHA_LESS_1) { + else if (t.prop & T_CONST_COLOR && t.prop & T_ALPHA_LESS_1) + { gl2ps->streamlength += gl2psPrintf("q\n" - "/GS%d gs\n", gro->gsno); - gl2ps->streamlength += gl2psPrintPDFFillColor( - prim->verts[0].rgba); - for (j = 0; j <= lastel; ++j) { - prim = *(GL2PSprimitive**) gl2psListPointer(gro->ptrlist, - j); - gl2psFillTriangleFromPrimitive(&t, prim, GL_FALSE); + "/GS%d gs\n", + gro->gsno) ; + gl2ps->streamlength += gl2psPrintPDFFillColor(prim->verts[0].rgba) ; + for (j = 0 ; j <= lastel; ++j) + { + prim = *(GL2PSprimitive**)gl2psListPointer(gro->ptrlist, j) ; + gl2psFillTriangleFromPrimitive(&t, prim, GL_FALSE) ; gl2ps->streamlength += gl2psPrintf("%f %f m\n" - "%f %f l\n" - "%f %f l\n" - "h f\n", t.vertex[0].xyz[0], t.vertex[0].xyz[1], - t.vertex[1].xyz[0], t.vertex[1].xyz[1], - t.vertex[2].xyz[0], t.vertex[2].xyz[1]); + "%f %f l\n" + "%f %f l\n" + "h f\n", + t.vertex[0].xyz[0], t.vertex[0].xyz[1], + t.vertex[1].xyz[0], t.vertex[1].xyz[1], + t.vertex[2].xyz[0], t.vertex[2].xyz[1]) ; } - gl2ps->streamlength += gl2psPrintf("Q\n"); + gl2ps->streamlength += gl2psPrintf("Q\n") ; } /* Variable alpha and const color: Simple PDF draw orders and an extra extended Graphics State + Xobject + Shader object for the alpha mask */ - else if (t.prop & T_CONST_COLOR && t.prop & T_VAR_ALPHA) { + else if (t.prop & T_CONST_COLOR && t.prop & T_VAR_ALPHA) + { gl2ps->streamlength += gl2psPrintf("q\n" - "/GS%d gs\n" - "/TrG%d Do\n", gro->gsno, gro->trgroupno); - gl2ps->streamlength += gl2psPrintPDFFillColor( - prim->verts[0].rgba); - for (j = 0; j <= lastel; ++j) { - prim = *(GL2PSprimitive**) gl2psListPointer(gro->ptrlist, - j); - gl2psFillTriangleFromPrimitive(&t, prim, GL_FALSE); + "/GS%d gs\n" + "/TrG%d Do\n", + gro->gsno, gro->trgroupno) ; + gl2ps->streamlength += gl2psPrintPDFFillColor(prim->verts[0].rgba) ; + for (j = 0 ; j <= lastel; ++j) + { + prim = *(GL2PSprimitive**)gl2psListPointer(gro->ptrlist, j) ; + gl2psFillTriangleFromPrimitive(&t, prim, GL_FALSE) ; gl2ps->streamlength += gl2psPrintf("%f %f m\n" - "%f %f l\n" - "%f %f l\n" - "h f\n", t.vertex[0].xyz[0], t.vertex[0].xyz[1], - t.vertex[1].xyz[0], t.vertex[1].xyz[1], - t.vertex[2].xyz[0], t.vertex[2].xyz[1]); + "%f %f l\n" + "%f %f l\n" + "h f\n", + t.vertex[0].xyz[0], t.vertex[0].xyz[1], + t.vertex[1].xyz[0], t.vertex[1].xyz[1], + t.vertex[2].xyz[0], t.vertex[2].xyz[1]) ; } - gl2ps->streamlength += gl2psPrintf("Q\n"); + gl2ps->streamlength += gl2psPrintf("Q\n") ; } /* Variable color and no alpha: Shader Object for the colored triangle(s) */ - else if (t.prop & T_VAR_COLOR && t.prop & T_ALPHA_1) { - gl2ps->streamlength += gl2psPrintf("/Sh%d sh\n", gro->shno); + else if (t.prop & T_VAR_COLOR && t.prop & T_ALPHA_1) + { + gl2ps->streamlength += gl2psPrintf("/Sh%d sh\n", gro->shno) ; } /* Variable color and const alpha < 1: Shader Object for the colored triangle(s) and an extra extended Graphics State for the alpha const */ - else if (t.prop & T_VAR_COLOR && t.prop & T_ALPHA_LESS_1) { + else if (t.prop & T_VAR_COLOR && t.prop & T_ALPHA_LESS_1) + { gl2ps->streamlength += gl2psPrintf("q\n" - "/GS%d gs\n" - "/Sh%d sh\n" - "Q\n", gro->gsno, gro->shno); + "/GS%d gs\n" + "/Sh%d sh\n" + "Q\n", + gro->gsno, gro->shno) ; } /* Variable alpha and color: Shader Object for the colored triangle(s) and an extra extended Graphics State + Xobject + Shader object for the alpha mask */ - else if (t.prop & T_VAR_COLOR && t.prop & T_VAR_ALPHA) { + else if (t.prop & T_VAR_COLOR && t.prop & T_VAR_ALPHA) + { gl2ps->streamlength += gl2psPrintf("q\n" - "/GS%d gs\n" - "/TrG%d Do\n" - "/Sh%d sh\n" - "Q\n", gro->gsno, gro->trgroupno, gro->shno); + "/GS%d gs\n" + "/TrG%d Do\n" + "/Sh%d sh\n" + "Q\n", + gro->gsno, gro->trgroupno, gro->shno) ; } - break; - case GL2PS_PIXMAP: - for (j = 0; j <= lastel; ++j) { - prim = *(GL2PSprimitive**) gl2psListPointer(gro->ptrlist, j); - gl2psPutPDFImage(prim->data.image, gro->imno, - prim->verts[0].xyz[0], prim->verts[0].xyz[1]); + break ; + case GL2PS_PIXMAP : + for (j = 0 ; j <= lastel; ++j) + { + prim = *(GL2PSprimitive**)gl2psListPointer(gro->ptrlist, j) ; + gl2psPutPDFImage(prim->data.image, gro->imno, prim->verts[0].xyz[0], + prim->verts[0].xyz[1]) ; } - break; - case GL2PS_TEXT: - for (j = 0; j <= lastel; ++j) { - prim = *(GL2PSprimitive**) gl2psListPointer(gro->ptrlist, j); - gl2ps->streamlength += gl2psPrintPDFFillColor( - prim->verts[0].rgba); - gl2psPutPDFText(prim->data.text, gro->fontno, - prim->verts[0].xyz[0], prim->verts[0].xyz[1]); + break ; + case GL2PS_TEXT : + for (j = 0 ; j <= lastel; ++j) + { + prim = *(GL2PSprimitive**)gl2psListPointer(gro->ptrlist, j) ; + gl2ps->streamlength += gl2psPrintPDFFillColor(prim->verts[0].rgba) ; + gl2psPutPDFText(prim->data.text, gro->fontno, prim->verts[0].xyz[0], + prim->verts[0].xyz[1]) ; } - break; - default: - break; + break ; + default : + break ; } } } /* Graphics State names */ -static int gl2psPDFgroupListWriteGStateResources(void) { - GL2PSpdfgroup *gro; - int offs = 0; - int i; +static int gl2psPDFgroupListWriteGStateResources(void) +{ + GL2PSpdfgroup *gro ; + int offs = 0 ; + int i ; offs += fprintf(gl2ps->stream, "/ExtGState\n" - "<<\n" - "/GSa 7 0 R\n"); - for (i = 0; i < gl2psListNbr(gl2ps->pdfgrouplist); ++i) { - gro = (GL2PSpdfgroup*) gl2psListPointer(gl2ps->pdfgrouplist, i); - if (gro->gsno >= 0) - offs += fprintf(gl2ps->stream, "/GS%d %d 0 R\n", gro->gsno, - gro->gsobjno); + "<<\n" + "/GSa 7 0 R\n") ; + for (i = 0 ; i < gl2psListNbr(gl2ps->pdfgrouplist); ++i) + { + gro = (GL2PSpdfgroup*)gl2psListPointer(gl2ps->pdfgrouplist, i) ; + if (gro->gsno >= 0) offs += fprintf(gl2ps->stream, "/GS%d %d 0 R\n", gro->gsno, + gro->gsobjno) ; } - offs += fprintf(gl2ps->stream, ">>\n"); - return offs; + offs += fprintf(gl2ps->stream, ">>\n") ; + return offs ; } /* Main Shader names */ -static int gl2psPDFgroupListWriteShaderResources(void) { - GL2PSpdfgroup *gro; - int offs = 0; - int i; +static int gl2psPDFgroupListWriteShaderResources(void) +{ + GL2PSpdfgroup *gro ; + int offs = 0 ; + int i ; offs += fprintf(gl2ps->stream, "/Shading\n" - "<<\n"); - for (i = 0; i < gl2psListNbr(gl2ps->pdfgrouplist); ++i) { - gro = (GL2PSpdfgroup*) gl2psListPointer(gl2ps->pdfgrouplist, i); - if (gro->shno >= 0) - offs += fprintf(gl2ps->stream, "/Sh%d %d 0 R\n", gro->shno, - gro->shobjno); - if (gro->maskshno >= 0) - offs += fprintf(gl2ps->stream, "/TrSh%d %d 0 R\n", gro->maskshno, - gro->maskshobjno); + "<<\n") ; + for (i = 0 ; i < gl2psListNbr(gl2ps->pdfgrouplist); ++i) + { + gro = (GL2PSpdfgroup*)gl2psListPointer(gl2ps->pdfgrouplist, i) ; + if (gro->shno >= 0) offs += fprintf(gl2ps->stream, "/Sh%d %d 0 R\n", gro->shno, + gro->shobjno) ; + if (gro->maskshno >= 0) offs += fprintf(gl2ps->stream, "/TrSh%d %d 0 R\n", gro->maskshno, + gro->maskshobjno) ; } - offs += fprintf(gl2ps->stream, ">>\n"); - return offs; + offs += fprintf(gl2ps->stream, ">>\n") ; + return offs ; } /* Images & Mask Shader XObject names */ -static int gl2psPDFgroupListWriteXObjectResources(void) { - int i; - GL2PSprimitive *p = NULL; - GL2PSpdfgroup *gro; - int offs = 0; +static int gl2psPDFgroupListWriteXObjectResources(void) +{ + int i ; + GL2PSprimitive *p = NULL ; + GL2PSpdfgroup *gro ; + int offs = 0 ; offs += fprintf(gl2ps->stream, "/XObject\n" - "<<\n"); - - for (i = 0; i < gl2psListNbr(gl2ps->pdfgrouplist); ++i) { - gro = (GL2PSpdfgroup*) gl2psListPointer(gl2ps->pdfgrouplist, i); - if (!gl2psListNbr(gro->ptrlist)) - continue; - p = *(GL2PSprimitive**) gl2psListPointer(gro->ptrlist, 0); - switch (p->type) { - case GL2PS_PIXMAP: - gro->imobjno = gl2ps->objects_stack++; + "<<\n") ; + + for (i = 0 ; i < gl2psListNbr(gl2ps->pdfgrouplist); ++i) + { + gro = (GL2PSpdfgroup*)gl2psListPointer(gl2ps->pdfgrouplist, i) ; + if (!gl2psListNbr(gro->ptrlist)) continue ; + p = *(GL2PSprimitive**)gl2psListPointer(gro->ptrlist, 0) ; + switch (p->type) + { + case GL2PS_PIXMAP : + gro->imobjno = gl2ps->objects_stack++ ; if (GL_RGBA == p->data.image->format) /* reserve one object for image mask */ - gl2ps->objects_stack++; - offs += fprintf(gl2ps->stream, "/Im%d %d 0 R\n", gro->imno, - gro->imobjno); - case GL2PS_TRIANGLE: - if (gro->trgroupno >= 0) - offs += fprintf(gl2ps->stream, "/TrG%d %d 0 R\n", - gro->trgroupno, gro->trgroupobjno); - break; - default: - break; + gl2ps->objects_stack++ ; + offs += fprintf(gl2ps->stream, "/Im%d %d 0 R\n", gro->imno, gro->imobjno) ; + case GL2PS_TRIANGLE : + if (gro->trgroupno >= 0) offs += fprintf(gl2ps->stream, "/TrG%d %d 0 R\n", + gro->trgroupno, gro->trgroupobjno) ; + break ; + default : + break ; } } - offs += fprintf(gl2ps->stream, ">>\n"); - return offs; + offs += fprintf(gl2ps->stream, ">>\n") ; + return offs ; } /* Font names */ -static int gl2psPDFgroupListWriteFontResources(void) { - int i; - GL2PSpdfgroup *gro; - int offs = 0; +static int gl2psPDFgroupListWriteFontResources(void) +{ + int i ; + GL2PSpdfgroup *gro ; + int offs = 0 ; - offs += fprintf(gl2ps->stream, "/Font\n<<\n"); + offs += fprintf(gl2ps->stream, "/Font\n<<\n") ; - for (i = 0; i < gl2psListNbr(gl2ps->pdfgrouplist); ++i) { - gro = (GL2PSpdfgroup*) gl2psListPointer(gl2ps->pdfgrouplist, i); - if (gro->fontno < 0) - continue; - gro->fontobjno = gl2ps->objects_stack++; - offs += fprintf(gl2ps->stream, "/F%d %d 0 R\n", gro->fontno, - gro->fontobjno); + for (i = 0 ; i < gl2psListNbr(gl2ps->pdfgrouplist); ++i) + { + gro = (GL2PSpdfgroup*)gl2psListPointer(gl2ps->pdfgrouplist, i) ; + if (gro->fontno < 0) continue ; + gro->fontobjno = gl2ps->objects_stack++ ; + offs += fprintf(gl2ps->stream, "/F%d %d 0 R\n", gro->fontno, gro->fontobjno) ; } - offs += fprintf(gl2ps->stream, ">>\n"); + offs += fprintf(gl2ps->stream, ">>\n") ; - return offs; + return offs ; } -static void gl2psPDFgroupListDelete(void) { - int i; - GL2PSpdfgroup *gro = NULL; +static void gl2psPDFgroupListDelete(void) +{ + int i ; + GL2PSpdfgroup *gro = NULL ; - if (!gl2ps->pdfgrouplist) - return; + if (!gl2ps->pdfgrouplist) return ; - for (i = 0; i < gl2psListNbr(gl2ps->pdfgrouplist); ++i) { - gro = (GL2PSpdfgroup*) gl2psListPointer(gl2ps->pdfgrouplist, i); - gl2psListDelete(gro->ptrlist); + for (i = 0 ; i < gl2psListNbr(gl2ps->pdfgrouplist); ++i) + { + gro = (GL2PSpdfgroup*)gl2psListPointer(gl2ps->pdfgrouplist, i) ; + gl2psListDelete(gro->ptrlist) ; } - gl2psListDelete(gl2ps->pdfgrouplist); - gl2ps->pdfgrouplist = NULL; + gl2psListDelete(gl2ps->pdfgrouplist) ; + gl2ps->pdfgrouplist = NULL ; } /* Print 1st PDF object - file info */ -static int gl2psPrintPDFInfo(void) { - int offs; - time_t now; - struct tm *newtime; +static int gl2psPrintPDFInfo(void) +{ + int offs ; + time_t now ; + struct tm *newtime ; - time(&now); - newtime = gmtime(&now); + time(&now) ; + newtime = gmtime(&now) ; offs = fprintf(gl2ps->stream, "1 0 obj\n" - "<<\n" - "/Title (%s)\n" - "/Creator (GL2PS %d.%d.%d%s, %s)\n" - "/Producer (%s)\n", gl2ps->title, GL2PS_MAJOR_VERSION, - GL2PS_MINOR_VERSION, GL2PS_PATCH_VERSION, GL2PS_EXTRA_VERSION, - GL2PS_COPYRIGHT, gl2ps->producer); - - if (!newtime) { + "<<\n" + "/Title (%s)\n" + "/Creator (GL2PS %d.%d.%d%s, %s)\n" + "/Producer (%s)\n", + gl2ps->title, GL2PS_MAJOR_VERSION, GL2PS_MINOR_VERSION, GL2PS_PATCH_VERSION, + GL2PS_EXTRA_VERSION, GL2PS_COPYRIGHT, gl2ps->producer) ; + + if (!newtime) + { offs += fprintf(gl2ps->stream, ">>\n" - "endobj\n"); - return offs; + "endobj\n") ; + return offs ; } offs += fprintf(gl2ps->stream, "/CreationDate (D:%d%02d%02d%02d%02d%02d)\n" - ">>\n" - "endobj\n", newtime->tm_year + 1900, newtime->tm_mon + 1, - newtime->tm_mday, newtime->tm_hour, newtime->tm_min, - newtime->tm_sec); - return offs; + ">>\n" + "endobj\n", + newtime->tm_year + 1900, newtime->tm_mon + 1, newtime->tm_mday, + newtime->tm_hour, newtime->tm_min, newtime->tm_sec) ; + return offs ; } /* Create catalog and page structure - 2nd and 3th PDF object */ -static int gl2psPrintPDFCatalog(void) { +static int gl2psPrintPDFCatalog(void) +{ return fprintf(gl2ps->stream, "2 0 obj\n" - "<<\n" - "/Type /Catalog\n" - "/Pages 3 0 R\n" - ">>\n" - "endobj\n"); + "<<\n" + "/Type /Catalog\n" + "/Pages 3 0 R\n" + ">>\n" + "endobj\n") ; } -static int gl2psPrintPDFPages(void) { +static int gl2psPrintPDFPages(void) +{ return fprintf(gl2ps->stream, "3 0 obj\n" - "<<\n" - "/Type /Pages\n" - "/Kids [6 0 R]\n" - "/Count 1\n" - ">>\n" - "endobj\n"); + "<<\n" + "/Type /Pages\n" + "/Kids [6 0 R]\n" + "/Count 1\n" + ">>\n" + "endobj\n") ; } /* Open stream for data - graphical objects, fonts etc. PDF object 4 */ -static int gl2psOpenPDFDataStream(void) { - int offs = 0; +static int gl2psOpenPDFDataStream(void) +{ + int offs = 0 ; offs += fprintf(gl2ps->stream, "4 0 obj\n" - "<<\n" - "/Length 5 0 R\n"); - offs += gl2psPrintPDFCompressorType(); + "<<\n" + "/Length 5 0 R\n") ; + offs += gl2psPrintPDFCompressorType() ; offs += fprintf(gl2ps->stream, ">>\n" - "stream\n"); - return offs; + "stream\n") ; + return offs ; } /* Stream setup - Graphics state, fill background if allowed */ -static int gl2psOpenPDFDataStreamWritePreface(void) { - int offs; +static int gl2psOpenPDFDataStreamWritePreface(void) +{ + int offs ; - offs = gl2psPrintf("/GSa gs\n"); + offs = gl2psPrintf("/GSa gs\n") ; - if (gl2ps->options & GL2PS_DRAW_BACKGROUND) { - offs += gl2psPrintPDFFillColor(gl2ps->bgcolor); - offs += gl2psPrintf("%d %d %d %d re\n", (int) gl2ps->viewport[0], - (int) gl2ps->viewport[1], (int) gl2ps->viewport[2], - (int) gl2ps->viewport[3]); - offs += gl2psPrintf("f\n"); + if (gl2ps->options & GL2PS_DRAW_BACKGROUND) + { + offs += gl2psPrintPDFFillColor(gl2ps->bgcolor) ; + offs += gl2psPrintf("%d %d %d %d re\n", (int)gl2ps->viewport[0], (int)gl2ps->viewport[1], + (int)gl2ps->viewport[2], (int)gl2ps->viewport[3]) ; + offs += gl2psPrintf("f\n") ; } - return offs; + return offs ; } /* Use the functions above to create the first part of the PDF*/ -static void gl2psPrintPDFHeader(void) { - int offs = 0; - gl2ps->pdfprimlist = gl2psListCreate(500, 500, sizeof(GL2PSprimitive*)); - gl2psPDFstacksInit(); +static void gl2psPrintPDFHeader(void) +{ + int offs = 0 ; + gl2ps->pdfprimlist = gl2psListCreate(500, 500, sizeof(GL2PSprimitive*)) ; + gl2psPDFstacksInit() ; - gl2ps->xreflist = (int*) gl2psMalloc(sizeof(int) * gl2ps->objects_stack); + gl2ps->xreflist = (int*)gl2psMalloc(sizeof(int) * gl2ps->objects_stack) ; #if defined(GL2PS_HAVE_ZLIB) - if(gl2ps->options & GL2PS_COMPRESS) { - gl2psSetupCompress(); + if(gl2ps->options & GL2PS_COMPRESS) + { + gl2psSetupCompress() ; } #endif - gl2ps->xreflist[0] = 0; - offs += fprintf(gl2ps->stream, "%%PDF-1.4\n"); - gl2ps->xreflist[1] = offs; + gl2ps->xreflist[0] = 0 ; + offs += fprintf(gl2ps->stream, "%%PDF-1.4\n") ; + gl2ps->xreflist[1] = offs ; - offs += gl2psPrintPDFInfo(); - gl2ps->xreflist[2] = offs; + offs += gl2psPrintPDFInfo() ; + gl2ps->xreflist[2] = offs ; - offs += gl2psPrintPDFCatalog(); - gl2ps->xreflist[3] = offs; + offs += gl2psPrintPDFCatalog() ; + gl2ps->xreflist[3] = offs ; - offs += gl2psPrintPDFPages(); - gl2ps->xreflist[4] = offs; + offs += gl2psPrintPDFPages() ; + gl2ps->xreflist[4] = offs ; - offs += gl2psOpenPDFDataStream(); - gl2ps->xreflist[5] = offs; /* finished in gl2psPrintPDFFooter */ - gl2ps->streamlength = gl2psOpenPDFDataStreamWritePreface(); + offs += gl2psOpenPDFDataStream() ; + gl2ps->xreflist[5] = offs ; /* finished in gl2psPrintPDFFooter */ + gl2ps->streamlength = gl2psOpenPDFDataStreamWritePreface() ; } /* The central primitive drawing */ -static void gl2psPrintPDFPrimitive(void *data) { - GL2PSprimitive *prim = *(GL2PSprimitive**) data; +static void gl2psPrintPDFPrimitive(void *data) +{ + GL2PSprimitive *prim = *(GL2PSprimitive**)data ; - if ((gl2ps->options & GL2PS_OCCLUSION_CULL) && prim->culled) - return; + if ((gl2ps->options & GL2PS_OCCLUSION_CULL) && prim->culled) return ; - prim = gl2psCopyPrimitive(prim); /* deep copy */ - gl2psListAdd(gl2ps->pdfprimlist, &prim); + prim = gl2psCopyPrimitive(prim) ; /* deep copy */ + gl2psListAdd(gl2ps->pdfprimlist, &prim) ; } /* close stream and ... */ -static int gl2psClosePDFDataStream(void) { - int offs = 0; +static int gl2psClosePDFDataStream(void) +{ + int offs = 0 ; #if defined(GL2PS_HAVE_ZLIB) - if(gl2ps->options & GL2PS_COMPRESS) { + if(gl2ps->options & GL2PS_COMPRESS) + { if(Z_OK != gl2psDeflate()) - gl2psMsg(GL2PS_ERROR, "Zlib deflate error"); + gl2psMsg(GL2PS_ERROR, "Zlib deflate error") ; else - fwrite(gl2ps->compress->dest, gl2ps->compress->destLen, 1, gl2ps->stream); - gl2ps->streamlength += gl2ps->compress->destLen; + fwrite(gl2ps->compress->dest, gl2ps->compress->destLen, 1, gl2ps->stream) ; + gl2ps->streamlength += gl2ps->compress->destLen ; - offs += gl2ps->streamlength; - gl2psFreeCompress(); + offs += gl2ps->streamlength ; + gl2psFreeCompress() ; } #endif offs += fprintf(gl2ps->stream, "endstream\n" - "endobj\n"); - return offs; + "endobj\n") ; + return offs ; } /* ... write the now known length object */ -static int gl2psPrintPDFDataStreamLength(int val) { +static int gl2psPrintPDFDataStreamLength(int val) +{ return fprintf(gl2ps->stream, "5 0 obj\n" - "%d\n" - "endobj\n", val); + "%d\n" + "endobj\n", + val) ; } /* Put the info created before in PDF objects */ -static int gl2psPrintPDFOpenPage(void) { - int offs; +static int gl2psPrintPDFOpenPage(void) +{ + int offs ; /* Write fixed part */ offs = fprintf(gl2ps->stream, "6 0 obj\n" - "<<\n" - "/Type /Page\n" - "/Parent 3 0 R\n" - "/MediaBox [%d %d %d %d]\n", (int) gl2ps->viewport[0], - (int) gl2ps->viewport[1], (int) gl2ps->viewport[2], - (int) gl2ps->viewport[3]); + "<<\n" + "/Type /Page\n" + "/Parent 3 0 R\n" + "/MediaBox [%d %d %d %d]\n", + (int)gl2ps->viewport[0], (int)gl2ps->viewport[1], (int)gl2ps->viewport[2], + (int)gl2ps->viewport[3]) ; - if (gl2ps->options & GL2PS_LANDSCAPE) - offs += fprintf(gl2ps->stream, "/Rotate -90\n"); + if (gl2ps->options & GL2PS_LANDSCAPE) offs += fprintf(gl2ps->stream, "/Rotate -90\n") ; offs += fprintf(gl2ps->stream, "/Contents 4 0 R\n" - "/Resources\n" - "<<\n" - "/ProcSet [/PDF /Text /ImageB /ImageC] %%/ImageI\n"); + "/Resources\n" + "<<\n" + "/ProcSet [/PDF /Text /ImageB /ImageC] %%/ImageI\n") ; - return offs; + return offs ; /* End fixed part, proceeds in gl2psPDFgroupListWriteVariableResources() */ } -static int gl2psPDFgroupListWriteVariableResources(void) { - int offs = 0; +static int gl2psPDFgroupListWriteVariableResources(void) +{ + int offs = 0 ; /* a) Graphics States for shader alpha masks*/ - offs += gl2psPDFgroupListWriteGStateResources(); + offs += gl2psPDFgroupListWriteGStateResources() ; /* b) Shader and shader masks */ - offs += gl2psPDFgroupListWriteShaderResources(); + offs += gl2psPDFgroupListWriteShaderResources() ; /* c) XObjects (Images & Shader Masks) */ - offs += gl2psPDFgroupListWriteXObjectResources(); + offs += gl2psPDFgroupListWriteXObjectResources() ; /* d) Fonts */ - offs += gl2psPDFgroupListWriteFontResources(); + offs += gl2psPDFgroupListWriteFontResources() ; /* End resources and page */ offs += fprintf(gl2ps->stream, ">>\n" - ">>\n" - "endobj\n"); - return offs; + ">>\n" + "endobj\n") ; + return offs ; } /* Standard Graphics State */ -static int gl2psPrintPDFGSObject(void) { +static int gl2psPrintPDFGSObject(void) +{ return fprintf(gl2ps->stream, "7 0 obj\n" - "<<\n" - "/Type /ExtGState\n" - "/SA false\n" - "/SM 0.02\n" - "/OP false\n" - "/op false\n" - "/OPM 0\n" - "/BG2 /Default\n" - "/UCR2 /Default\n" - "/TR2 /Default\n" - ">>\n" - "endobj\n"); + "<<\n" + "/Type /ExtGState\n" + "/SA false\n" + "/SM 0.02\n" + "/OP false\n" + "/op false\n" + "/OPM 0\n" + "/BG2 /Default\n" + "/UCR2 /Default\n" + "/TR2 /Default\n" + ">>\n" + "endobj\n") ; } /* Put vertex' edge flag (8bit) and coordinates (32bit) in shader stream */ static int gl2psPrintPDFShaderStreamDataCoord(GL2PSvertex *vertex, - size_t(*action)(unsigned long data, size_t size), GLfloat dx, - GLfloat dy, GLfloat xmin, GLfloat ymin) { - int offs = 0; - unsigned long imap; - GLfloat diff; - double dmax = ~1UL; - char edgeflag = 0; + size_t(*action)(unsigned long data, size_t size), + GLfloat dx, GLfloat dy, GLfloat xmin, GLfloat ymin) +{ + int offs = 0 ; + unsigned long imap ; + GLfloat diff ; + double dmax = ~1UL ; + char edgeflag = 0 ; /* FIXME: temp bux fix for 64 bit archs: */ - if (sizeof(unsigned long) == 8) - dmax = dmax - 2048.; + if (sizeof(unsigned long) == 8) dmax = dmax - 2048. ; - offs += (*action)(edgeflag, 1); + offs += (*action)(edgeflag, 1) ; /* The Shader stream in PDF requires to be in a 'big-endian' order */ - if (GL2PS_ZERO(dx * dy)) { - offs += (*action)(0, 4); - offs += (*action)(0, 4); - } else { - diff = (vertex->xyz[0] - xmin) / dx; + if (GL2PS_ZERO(dx * dy)) + { + offs += (*action)(0, 4) ; + offs += (*action)(0, 4) ; + } + else + { + diff = (vertex->xyz[0] - xmin) / dx ; if (diff > 1) - diff = 1.0F; - else if (diff < 0) - diff = 0.0F; - imap = (unsigned long) (diff * dmax); - offs += (*action)(imap, 4); + diff = 1.0F ; + else if (diff < 0) diff = 0.0F ; + imap = (unsigned long)(diff * dmax) ; + offs += (*action)(imap, 4) ; - diff = (vertex->xyz[1] - ymin) / dy; + diff = (vertex->xyz[1] - ymin) / dy ; if (diff > 1) - diff = 1.0F; - else if (diff < 0) - diff = 0.0F; - imap = (unsigned long) (diff * dmax); - offs += (*action)(imap, 4); + diff = 1.0F ; + else if (diff < 0) diff = 0.0F ; + imap = (unsigned long)(diff * dmax) ; + offs += (*action)(imap, 4) ; } - return offs; + return offs ; } /* Put vertex' rgb value (8bit for every component) in shader stream */ static int gl2psPrintPDFShaderStreamDataRGB(GL2PSvertex *vertex, - size_t(*action)(unsigned long data, size_t size)) { - int offs = 0; - unsigned long imap; - double dmax = ~1UL; + size_t(*action)(unsigned long data, size_t size)) +{ + int offs = 0 ; + unsigned long imap ; + double dmax = ~1UL ; /* FIXME: temp bux fix for 64 bit archs: */ - if (sizeof(unsigned long) == 8) - dmax = dmax - 2048.; + if (sizeof(unsigned long) == 8) dmax = dmax - 2048. ; - imap = (unsigned long) ((vertex->rgba[0]) * dmax); - offs += (*action)(imap, 1); + imap = (unsigned long)((vertex->rgba[0]) * dmax) ; + offs += (*action)(imap, 1) ; - imap = (unsigned long) ((vertex->rgba[1]) * dmax); - offs += (*action)(imap, 1); + imap = (unsigned long)((vertex->rgba[1]) * dmax) ; + offs += (*action)(imap, 1) ; - imap = (unsigned long) ((vertex->rgba[2]) * dmax); - offs += (*action)(imap, 1); + imap = (unsigned long)((vertex->rgba[2]) * dmax) ; + offs += (*action)(imap, 1) ; - return offs; + return offs ; } /* Put vertex' alpha (8/16bit) in shader stream */ static int gl2psPrintPDFShaderStreamDataAlpha(GL2PSvertex *vertex, - size_t(*action)(unsigned long data, size_t size), int sigbyte) { - int offs = 0; - unsigned long imap; - double dmax = ~1UL; + size_t(*action)(unsigned long data, size_t size), + int sigbyte) +{ + int offs = 0 ; + unsigned long imap ; + double dmax = ~1UL ; /* FIXME: temp bux fix for 64 bit archs: */ - if (sizeof(unsigned long) == 8) - dmax = dmax - 2048.; + if (sizeof(unsigned long) == 8) dmax = dmax - 2048. ; - if (sigbyte != 8 && sigbyte != 16) - sigbyte = 8; + if (sigbyte != 8 && sigbyte != 16) sigbyte = 8 ; - sigbyte /= 8; + sigbyte /= 8 ; - imap = (unsigned long) ((vertex->rgba[3]) * dmax); + imap = (unsigned long)((vertex->rgba[3]) * dmax) ; - offs += (*action)(imap, sigbyte); + offs += (*action)(imap, sigbyte) ; - return offs; + return offs ; } /* Put a triangles raw data in shader stream */ -static int gl2psPrintPDFShaderStreamData(GL2PStriangle *triangle, GLfloat dx, - GLfloat dy, GLfloat xmin, GLfloat ymin, - size_t(*action)(unsigned long data, size_t size), int gray) { - int i, offs = 0; - GL2PSvertex v; - - if (gray && gray != 8 && gray != 16) - gray = 8; - - for (i = 0; i < 3; ++i) { - offs += gl2psPrintPDFShaderStreamDataCoord(&triangle->vertex[i], action, - dx, dy, xmin, ymin); - if (gray) { - v = triangle->vertex[i]; - offs += gl2psPrintPDFShaderStreamDataAlpha(&v, action, gray); - } else { - offs += gl2psPrintPDFShaderStreamDataRGB(&triangle->vertex[i], - action); +static int gl2psPrintPDFShaderStreamData(GL2PStriangle *triangle, GLfloat dx, GLfloat dy, + GLfloat xmin, GLfloat ymin, + size_t(*action)(unsigned long data, size_t size), int gray) +{ + int i, offs = 0 ; + GL2PSvertex v ; + + if (gray && gray != 8 && gray != 16) gray = 8 ; + + for (i = 0 ; i < 3; ++i) + { + offs += gl2psPrintPDFShaderStreamDataCoord(&triangle->vertex[i], action, dx, dy, xmin, + ymin) ; + if (gray) + { + v = triangle->vertex[i] ; + offs += gl2psPrintPDFShaderStreamDataAlpha(&v, action, gray) ; + } + else + { + offs += gl2psPrintPDFShaderStreamDataRGB(&triangle->vertex[i], action) ; } } - return offs; + return offs ; } -static void gl2psPDFRectHull(GLfloat *xmin, GLfloat *xmax, GLfloat *ymin, - GLfloat *ymax, GL2PStriangle *triangles, int cnt) { - int i, j; - - *xmin = triangles[0].vertex[0].xyz[0]; - *xmax = triangles[0].vertex[0].xyz[0]; - *ymin = triangles[0].vertex[0].xyz[1]; - *ymax = triangles[0].vertex[0].xyz[1]; - - for (i = 0; i < cnt; ++i) { - for (j = 0; j < 3; ++j) { - if (*xmin > triangles[i].vertex[j].xyz[0]) - *xmin = triangles[i].vertex[j].xyz[0]; - if (*xmax < triangles[i].vertex[j].xyz[0]) - *xmax = triangles[i].vertex[j].xyz[0]; - if (*ymin > triangles[i].vertex[j].xyz[1]) - *ymin = triangles[i].vertex[j].xyz[1]; - if (*ymax < triangles[i].vertex[j].xyz[1]) - *ymax = triangles[i].vertex[j].xyz[1]; +static void gl2psPDFRectHull(GLfloat *xmin, GLfloat *xmax, GLfloat *ymin, GLfloat *ymax, + GL2PStriangle *triangles, int cnt) +{ + int i, j ; + + *xmin = triangles[0].vertex[0].xyz[0] ; + *xmax = triangles[0].vertex[0].xyz[0] ; + *ymin = triangles[0].vertex[0].xyz[1] ; + *ymax = triangles[0].vertex[0].xyz[1] ; + + for (i = 0 ; i < cnt; ++i) + { + for (j = 0 ; j < 3; ++j) + { + if (*xmin > triangles[i].vertex[j].xyz[0]) *xmin = triangles[i].vertex[j].xyz[0] ; + if (*xmax < triangles[i].vertex[j].xyz[0]) *xmax = triangles[i].vertex[j].xyz[0] ; + if (*ymin > triangles[i].vertex[j].xyz[1]) *ymin = triangles[i].vertex[j].xyz[1] ; + if (*ymax < triangles[i].vertex[j].xyz[1]) *ymax = triangles[i].vertex[j].xyz[1] ; } } } @@ -4223,458 +4569,493 @@ static void gl2psPDFRectHull(GLfloat *xmin, GLfloat *xmax, GLfloat *ymin, gray == 8 8bit-grayscale (for alpha masks) gray == 16 16bit-grayscale (for alpha masks) */ -static int gl2psPrintPDFShader(int obj, GL2PStriangle *triangles, int size, - int gray) { - int i, offs = 0, vertexbytes, done = 0; - GLfloat xmin, xmax, ymin, ymax; - - switch (gray) { - case 0: - vertexbytes = 1 + 4 + 4 + 1 + 1 + 1; - break; - case 8: - vertexbytes = 1 + 4 + 4 + 1; - break; - case 16: - vertexbytes = 1 + 4 + 4 + 2; - break; - default: - gray = 8; - vertexbytes = 1 + 4 + 4 + 1; - break; - } - - gl2psPDFRectHull(&xmin, &xmax, &ymin, &ymax, triangles, size); +static int gl2psPrintPDFShader(int obj, GL2PStriangle *triangles, int size, int gray) +{ + int i, offs = 0, vertexbytes, done = 0 ; + GLfloat xmin, xmax, ymin, ymax ; + + switch (gray) + { + case 0 : + vertexbytes = 1 + 4 + 4 + 1 + 1 + 1 ; + break ; + case 8 : + vertexbytes = 1 + 4 + 4 + 1 ; + break ; + case 16 : + vertexbytes = 1 + 4 + 4 + 2 ; + break ; + default : + gray = 8 ; + vertexbytes = 1 + 4 + 4 + 1 ; + break ; + } + + gl2psPDFRectHull(&xmin, &xmax, &ymin, &ymax, triangles, size) ; offs += fprintf(gl2ps->stream, "%d 0 obj\n" - "<< " - "/ShadingType 4 " - "/ColorSpace %s " - "/BitsPerCoordinate 32 " - "/BitsPerComponent %d " - "/BitsPerFlag 8 " - "/Decode [%f %f %f %f 0 1 %s] ", obj, - (gray) ? "/DeviceGray" : "/DeviceRGB", (gray) ? gray : 8, xmin, - xmax, ymin, ymax, (gray) ? "" : "0 1 0 1"); + "<< " + "/ShadingType 4 " + "/ColorSpace %s " + "/BitsPerCoordinate 32 " + "/BitsPerComponent %d " + "/BitsPerFlag 8 " + "/Decode [%f %f %f %f 0 1 %s] ", + obj, (gray) ? "/DeviceGray" : "/DeviceRGB", (gray) ? gray : 8, xmin, xmax, ymin, + ymax, (gray) ? "" : "0 1 0 1") ; #if defined(GL2PS_HAVE_ZLIB) - if(gl2ps->options & GL2PS_COMPRESS) { - gl2psAllocCompress(vertexbytes * size * 3); + if(gl2ps->options & GL2PS_COMPRESS) + { + gl2psAllocCompress(vertexbytes * size * 3) ; - for(i = 0; i < size; ++i) + for(i = 0 ; i < size ; ++i) gl2psPrintPDFShaderStreamData(&triangles[i], - xmax-xmin, ymax-ymin, xmin, ymin, - gl2psWriteBigEndianCompress, gray); + xmax-xmin, ymax-ymin, xmin, ymin, + gl2psWriteBigEndianCompress, gray) ; - if(Z_OK == gl2psDeflate() && 23 + gl2ps->compress->destLen < gl2ps->compress->srcLen) { - offs += gl2psPrintPDFCompressorType(); + if(Z_OK == gl2psDeflate() && 23 + gl2ps->compress->destLen < gl2ps->compress->srcLen) + { + offs += gl2psPrintPDFCompressorType() ; offs += fprintf(gl2ps->stream, - "/Length %d " - ">>\n" - "stream\n", - (int)gl2ps->compress->destLen); + "/Length %d " + ">>\n" + "stream\n", + (int)gl2ps->compress->destLen) ; offs += gl2ps->compress->destLen * fwrite(gl2ps->compress->dest, - gl2ps->compress->destLen, - 1, gl2ps->stream); - done = 1; + gl2ps->compress->destLen, + 1, gl2ps->stream) ; + done = 1 ; } - gl2psFreeCompress(); + gl2psFreeCompress() ; } #endif - if (!done) { + if (!done) + { /* no compression, or too long after compression, or compress error -> write non-compressed entry */ offs += fprintf(gl2ps->stream, "/Length %d " - ">>\n" - "stream\n", vertexbytes * 3 * size); - for (i = 0; i < size; ++i) - offs += gl2psPrintPDFShaderStreamData(&triangles[i], xmax - xmin, - ymax - ymin, xmin, ymin, gl2psWriteBigEndian, gray); + ">>\n" + "stream\n", + vertexbytes * 3 * size) ; + for (i = 0 ; i < size; ++i) + offs += gl2psPrintPDFShaderStreamData(&triangles[i], xmax - xmin, ymax - ymin, xmin, + ymin, gl2psWriteBigEndian, gray) ; } offs += fprintf(gl2ps->stream, "\nendstream\n" - "endobj\n"); + "endobj\n") ; - return offs; + return offs ; } /* Writes a XObject for a shaded triangle mask */ -static int gl2psPrintPDFShaderMask(int obj, int childobj) { - int offs = 0, len; +static int gl2psPrintPDFShaderMask(int obj, int childobj) +{ + int offs = 0, len ; offs += fprintf(gl2ps->stream, "%d 0 obj\n" - "<<\n" - "/Type /XObject\n" - "/Subtype /Form\n" - "/BBox [ %d %d %d %d ]\n" - "/Group \n<<\n/S /Transparency /CS /DeviceRGB\n" - ">>\n", obj, (int) gl2ps->viewport[0], (int) gl2ps->viewport[1], - (int) gl2ps->viewport[2], (int) gl2ps->viewport[3]); - - len = (childobj > 0) ? - strlen("/TrSh sh\n") + (int) log10((double) childobj) + 1 : - strlen("/TrSh0 sh\n"); + "<<\n" + "/Type /XObject\n" + "/Subtype /Form\n" + "/BBox [ %d %d %d %d ]\n" + "/Group \n<<\n/S /Transparency /CS /DeviceRGB\n" + ">>\n", + obj, (int)gl2ps->viewport[0], (int)gl2ps->viewport[1], (int)gl2ps->viewport[2], + (int)gl2ps->viewport[3]) ; + + len = + (childobj > 0) ? strlen("/TrSh sh\n") + (int)log10((double)childobj) + 1 : strlen( + "/TrSh0 sh\n") ; offs += fprintf(gl2ps->stream, "/Length %d\n" - ">>\n" - "stream\n", len); - offs += fprintf(gl2ps->stream, "/TrSh%d sh\n", childobj); + ">>\n" + "stream\n", + len) ; + offs += fprintf(gl2ps->stream, "/TrSh%d sh\n", childobj) ; offs += fprintf(gl2ps->stream, "endstream\n" - "endobj\n"); + "endobj\n") ; - return offs; + return offs ; } /* Writes a Extended graphics state for a shaded triangle mask if simplealpha ist true the childobj argument is ignored and a /ca statement will be written instead */ -static int gl2psPrintPDFShaderExtGS(int obj, int childobj) { - int offs = 0; +static int gl2psPrintPDFShaderExtGS(int obj, int childobj) +{ + int offs = 0 ; offs += fprintf(gl2ps->stream, "%d 0 obj\n" - "<<\n", obj); + "<<\n", + obj) ; - offs += fprintf(gl2ps->stream, "/SMask << /S /Alpha /G %d 0 R >> ", - childobj); + offs += fprintf(gl2ps->stream, "/SMask << /S /Alpha /G %d 0 R >> ", childobj) ; offs += fprintf(gl2ps->stream, ">>\n" - "endobj\n"); - return offs; + "endobj\n") ; + return offs ; } /* a simple graphics state */ -static int gl2psPrintPDFShaderSimpleExtGS(int obj, GLfloat alpha) { - int offs = 0; +static int gl2psPrintPDFShaderSimpleExtGS(int obj, GLfloat alpha) +{ + int offs = 0 ; offs += fprintf(gl2ps->stream, "%d 0 obj\n" - "<<\n" - "/ca %g" - ">>\n" - "endobj\n", obj, alpha); - return offs; + "<<\n" + "/ca %g" + ">>\n" + "endobj\n", + obj, alpha) ; + return offs ; } /* Similar groups of functions for pixmaps and text */ static int gl2psPrintPDFPixmapStreamData(GL2PSimage *im, - size_t(*action)(unsigned long data, size_t size), int gray) { - int x, y, shift; - GLfloat r, g, b, a; + size_t(*action)(unsigned long data, size_t size), int gray) +{ + int x, y, shift ; + GLfloat r, g, b, a ; - if (im->format != GL_RGBA && gray) - return 0; + if (im->format != GL_RGBA && gray) return 0 ; - if (gray && gray != 8 && gray != 16) - gray = 8; + if (gray && gray != 8 && gray != 16) gray = 8 ; - gray /= 8; + gray /= 8 ; - shift = (sizeof(unsigned long) - 1) * 8; + shift = (sizeof(unsigned long) - 1) * 8 ; - for (y = 0; y < im->height; ++y) { - for (x = 0; x < im->width; ++x) { - a = gl2psGetRGB(im, x, y, &r, &g, &b); - if (im->format == GL_RGBA && gray) { - (*action)((unsigned long) (a * 255) << shift, gray); - } else { - (*action)((unsigned long) (r * 255) << shift, 1); - (*action)((unsigned long) (g * 255) << shift, 1); - (*action)((unsigned long) (b * 255) << shift, 1); + for (y = 0 ; y < im->height; ++y) + { + for (x = 0 ; x < im->width; ++x) + { + a = gl2psGetRGB(im, x, y, &r, &g, &b) ; + if (im->format == GL_RGBA && gray) + { + (*action)((unsigned long)(a * 255) << shift, gray) ; + } + else + { + (*action)((unsigned long)(r * 255) << shift, 1) ; + (*action)((unsigned long)(g * 255) << shift, 1) ; + (*action)((unsigned long)(b * 255) << shift, 1) ; } } } - switch (gray) { - case 0: - return 3 * im->width * im->height; - case 1: - return im->width * im->height; - case 2: - return 2 * im->width * im->height; - default: - return 3 * im->width * im->height; + switch (gray) + { + case 0 : + return 3 * im->width * im->height ; + case 1 : + return im->width * im->height ; + case 2 : + return 2 * im->width * im->height ; + default : + return 3 * im->width * im->height ; } } -static int gl2psPrintPDFPixmap(int obj, int childobj, GL2PSimage *im, int gray) { - int offs = 0, done = 0, sigbytes = 3; +static int gl2psPrintPDFPixmap(int obj, int childobj, GL2PSimage *im, int gray) +{ + int offs = 0, done = 0, sigbytes = 3 ; - if (gray && gray != 8 && gray != 16) - gray = 8; + if (gray && gray != 8 && gray != 16) gray = 8 ; - if (gray) - sigbytes = gray / 8; + if (gray) sigbytes = gray / 8 ; offs += fprintf(gl2ps->stream, "%d 0 obj\n" - "<<\n" - "/Type /XObject\n" - "/Subtype /Image\n" - "/Width %d\n" - "/Height %d\n" - "/ColorSpace %s \n" - "/BitsPerComponent 8\n", obj, (int) im->width, (int) im->height, - (gray) ? "/DeviceGray" : "/DeviceRGB"); - if (GL_RGBA == im->format && gray == 0) { - offs += fprintf(gl2ps->stream, "/SMask %d 0 R\n", childobj); + "<<\n" + "/Type /XObject\n" + "/Subtype /Image\n" + "/Width %d\n" + "/Height %d\n" + "/ColorSpace %s \n" + "/BitsPerComponent 8\n", + obj, (int)im->width, (int)im->height, (gray) ? "/DeviceGray" : "/DeviceRGB") ; + if (GL_RGBA == im->format && gray == 0) + { + offs += fprintf(gl2ps->stream, "/SMask %d 0 R\n", childobj) ; } #if defined(GL2PS_HAVE_ZLIB) - if(gl2ps->options & GL2PS_COMPRESS) { - gl2psAllocCompress((int)(im->width * im->height * sigbytes)); + if(gl2ps->options & GL2PS_COMPRESS) + { + gl2psAllocCompress((int)(im->width * im->height * sigbytes)) ; - gl2psPrintPDFPixmapStreamData(im, gl2psWriteBigEndianCompress, gray); + gl2psPrintPDFPixmapStreamData(im, gl2psWriteBigEndianCompress, gray) ; - if(Z_OK == gl2psDeflate() && 23 + gl2ps->compress->destLen < gl2ps->compress->srcLen) { - offs += gl2psPrintPDFCompressorType(); + if(Z_OK == gl2psDeflate() && 23 + gl2ps->compress->destLen < gl2ps->compress->srcLen) + { + offs += gl2psPrintPDFCompressorType() ; offs += fprintf(gl2ps->stream, - "/Length %d " - ">>\n" - "stream\n", - (int)gl2ps->compress->destLen); + "/Length %d " + ">>\n" + "stream\n", + (int)gl2ps->compress->destLen) ; offs += gl2ps->compress->destLen * fwrite(gl2ps->compress->dest, gl2ps->compress->destLen, - 1, gl2ps->stream); - done = 1; + 1, gl2ps->stream) ; + done = 1 ; } - gl2psFreeCompress(); + gl2psFreeCompress() ; } #endif - if (!done) { + if (!done) + { /* no compression, or too long after compression, or compress error -> write non-compressed entry */ offs += fprintf(gl2ps->stream, "/Length %d " - ">>\n" - "stream\n", (int) (im->width * im->height * sigbytes)); - offs += gl2psPrintPDFPixmapStreamData(im, gl2psWriteBigEndian, gray); + ">>\n" + "stream\n", + (int)(im->width * im->height * sigbytes)) ; + offs += gl2psPrintPDFPixmapStreamData(im, gl2psWriteBigEndian, gray) ; } offs += fprintf(gl2ps->stream, "\nendstream\n" - "endobj\n"); + "endobj\n") ; - return offs; + return offs ; } -static int gl2psPrintPDFText(int obj, GL2PSstring *s, int fontnumber) { - int offs = 0; +static int gl2psPrintPDFText(int obj, GL2PSstring *s, int fontnumber) +{ + int offs = 0 ; offs += fprintf(gl2ps->stream, "%d 0 obj\n" - "<<\n" - "/Type /Font\n" - "/Subtype /Type1\n" - "/Name /F%d\n" - "/BaseFont /%s\n" - "/Encoding /MacRomanEncoding\n" - ">>\n" - "endobj\n", obj, fontnumber, s->fontname); - return offs; + "<<\n" + "/Type /Font\n" + "/Subtype /Type1\n" + "/Name /F%d\n" + "/BaseFont /%s\n" + "/Encoding /MacRomanEncoding\n" + ">>\n" + "endobj\n", + obj, fontnumber, s->fontname) ; + return offs ; } /* Write the physical objects */ -static int gl2psPDFgroupListWriteObjects(int entryoffs) { - int i, j; - GL2PSprimitive *p = NULL; - GL2PSpdfgroup *gro; - int offs = entryoffs; - GL2PStriangle *triangles; - int size = 0; - - if (!gl2ps->pdfgrouplist) - return offs; - - for (i = 0; i < gl2psListNbr(gl2ps->pdfgrouplist); ++i) { - gro = (GL2PSpdfgroup*) gl2psListPointer(gl2ps->pdfgrouplist, i); - if (!gl2psListNbr(gro->ptrlist)) - continue; - p = *(GL2PSprimitive**) gl2psListPointer(gro->ptrlist, 0); - switch (p->type) { - case GL2PS_POINT: - break; - case GL2PS_LINE: - break; - case GL2PS_TRIANGLE: - size = gl2psListNbr(gro->ptrlist); - triangles = (GL2PStriangle*) gl2psMalloc( - sizeof(GL2PStriangle) * size); - for (j = 0; j < size; ++j) { - p = *(GL2PSprimitive**) gl2psListPointer(gro->ptrlist, j); - gl2psFillTriangleFromPrimitive(&triangles[j], p, GL_TRUE); +static int gl2psPDFgroupListWriteObjects(int entryoffs) +{ + int i, j ; + GL2PSprimitive *p = NULL ; + GL2PSpdfgroup *gro ; + int offs = entryoffs ; + GL2PStriangle *triangles ; + int size = 0 ; + + if (!gl2ps->pdfgrouplist) return offs ; + + for (i = 0 ; i < gl2psListNbr(gl2ps->pdfgrouplist); ++i) + { + gro = (GL2PSpdfgroup*)gl2psListPointer(gl2ps->pdfgrouplist, i) ; + if (!gl2psListNbr(gro->ptrlist)) continue ; + p = *(GL2PSprimitive**)gl2psListPointer(gro->ptrlist, 0) ; + switch (p->type) + { + case GL2PS_POINT : + break ; + case GL2PS_LINE : + break ; + case GL2PS_TRIANGLE : + size = gl2psListNbr(gro->ptrlist) ; + triangles = (GL2PStriangle*)gl2psMalloc(sizeof(GL2PStriangle) * size) ; + for (j = 0 ; j < size; ++j) + { + p = *(GL2PSprimitive**)gl2psListPointer(gro->ptrlist, j) ; + gl2psFillTriangleFromPrimitive(&triangles[j], p, GL_TRUE) ; } - if (triangles[0].prop & T_VAR_COLOR) { - gl2ps->xreflist[gro->shobjno] = offs; - offs += gl2psPrintPDFShader(gro->shobjno, triangles, size, 0); + if (triangles[0].prop & T_VAR_COLOR) + { + gl2ps->xreflist[gro->shobjno] = offs ; + offs += gl2psPrintPDFShader(gro->shobjno, triangles, size, 0) ; } - if (triangles[0].prop & T_ALPHA_LESS_1) { - gl2ps->xreflist[gro->gsobjno] = offs; + if (triangles[0].prop & T_ALPHA_LESS_1) + { + gl2ps->xreflist[gro->gsobjno] = offs ; offs += gl2psPrintPDFShaderSimpleExtGS(gro->gsobjno, - triangles[0].vertex[0].rgba[3]); + triangles[0].vertex[0].rgba[3]) ; } - if (triangles[0].prop & T_VAR_ALPHA) { - gl2ps->xreflist[gro->gsobjno] = offs; - offs += gl2psPrintPDFShaderExtGS(gro->gsobjno, - gro->trgroupobjno); - gl2ps->xreflist[gro->trgroupobjno] = offs; - offs += gl2psPrintPDFShaderMask(gro->trgroupobjno, - gro->maskshno); - gl2ps->xreflist[gro->maskshobjno] = offs; - offs += gl2psPrintPDFShader(gro->maskshobjno, triangles, size, - 8); + if (triangles[0].prop & T_VAR_ALPHA) + { + gl2ps->xreflist[gro->gsobjno] = offs ; + offs += gl2psPrintPDFShaderExtGS(gro->gsobjno, gro->trgroupobjno) ; + gl2ps->xreflist[gro->trgroupobjno] = offs ; + offs += gl2psPrintPDFShaderMask(gro->trgroupobjno, gro->maskshno) ; + gl2ps->xreflist[gro->maskshobjno] = offs ; + offs += gl2psPrintPDFShader(gro->maskshobjno, triangles, size, 8) ; } - gl2psFree(triangles); - break; - case GL2PS_PIXMAP: - gl2ps->xreflist[gro->imobjno] = offs; - offs += gl2psPrintPDFPixmap(gro->imobjno, gro->imobjno + 1, - p->data.image, 0); - if (p->data.image->format == GL_RGBA) { - gl2ps->xreflist[gro->imobjno + 1] = offs; - offs += gl2psPrintPDFPixmap(gro->imobjno + 1, -1, p->data.image, - 8); + gl2psFree(triangles) ; + break ; + case GL2PS_PIXMAP : + gl2ps->xreflist[gro->imobjno] = offs ; + offs += gl2psPrintPDFPixmap(gro->imobjno, gro->imobjno + 1, p->data.image, 0) ; + if (p->data.image->format == GL_RGBA) + { + gl2ps->xreflist[gro->imobjno + 1] = offs ; + offs += gl2psPrintPDFPixmap(gro->imobjno + 1, -1, p->data.image, 8) ; } - break; - case GL2PS_TEXT: - gl2ps->xreflist[gro->fontobjno] = offs; - offs += gl2psPrintPDFText(gro->fontobjno, p->data.text, - gro->fontno); - break; - case GL2PS_SPECIAL: + break ; + case GL2PS_TEXT : + gl2ps->xreflist[gro->fontobjno] = offs ; + offs += gl2psPrintPDFText(gro->fontobjno, p->data.text, gro->fontno) ; + break ; + case GL2PS_SPECIAL : /* alignment contains the format for which the special output text is intended */ - if (p->data.text->alignment == GL2PS_PDF) - offs += fprintf(gl2ps->stream, "%s\n", p->data.text->str); - break; - default: - break; + if (p->data.text->alignment == GL2PS_PDF) offs += fprintf(gl2ps->stream, "%s\n", + p->data.text->str) ; + break ; + default : + break ; } } - return offs; + return offs ; } /* All variable data has been written at this point and all required functioninality has been gathered, so we can write now file footer with cross reference table and trailer */ -static void gl2psPrintPDFFooter(void) { - int i, offs; +static void gl2psPrintPDFFooter(void) +{ + int i, offs ; - gl2psPDFgroupListInit(); - gl2psPDFgroupListWriteMainStream(); + gl2psPDFgroupListInit() ; + gl2psPDFgroupListWriteMainStream() ; - offs = gl2ps->xreflist[5] + gl2ps->streamlength; - offs += gl2psClosePDFDataStream(); - gl2ps->xreflist[5] = offs; + offs = gl2ps->xreflist[5] + gl2ps->streamlength ; + offs += gl2psClosePDFDataStream() ; + gl2ps->xreflist[5] = offs ; - offs += gl2psPrintPDFDataStreamLength(gl2ps->streamlength); - gl2ps->xreflist[6] = offs; - gl2ps->streamlength = 0; + offs += gl2psPrintPDFDataStreamLength(gl2ps->streamlength) ; + gl2ps->xreflist[6] = offs ; + gl2ps->streamlength = 0 ; - offs += gl2psPrintPDFOpenPage(); - offs += gl2psPDFgroupListWriteVariableResources(); - gl2ps->xreflist = (int*) gl2psRealloc(gl2ps->xreflist, - sizeof(int) * (gl2ps->objects_stack + 1)); - gl2ps->xreflist[7] = offs; + offs += gl2psPrintPDFOpenPage() ; + offs += gl2psPDFgroupListWriteVariableResources() ; + gl2ps->xreflist = (int*)gl2psRealloc(gl2ps->xreflist, + sizeof(int) * (gl2ps->objects_stack + 1)) ; + gl2ps->xreflist[7] = offs ; - offs += gl2psPrintPDFGSObject(); - gl2ps->xreflist[8] = offs; + offs += gl2psPrintPDFGSObject() ; + gl2ps->xreflist[8] = offs ; - gl2ps->xreflist[gl2ps->objects_stack] = gl2psPDFgroupListWriteObjects( - gl2ps->xreflist[8]); + gl2ps->xreflist[gl2ps->objects_stack] = gl2psPDFgroupListWriteObjects(gl2ps->xreflist[8]) ; /* Start cross reference table. The file has to been opened in binary mode to preserve the 20 digit string length! */ fprintf(gl2ps->stream, "xref\n" - "0 %d\n" - "%010d 65535 f \n", gl2ps->objects_stack, 0); + "0 %d\n" + "%010d 65535 f \n", + gl2ps->objects_stack, 0) ; - for (i = 1; i < gl2ps->objects_stack; ++i) - fprintf(gl2ps->stream, "%010d 00000 n \n", gl2ps->xreflist[i]); + for (i = 1 ; i < gl2ps->objects_stack; ++i) + fprintf(gl2ps->stream, "%010d 00000 n \n", gl2ps->xreflist[i]) ; fprintf(gl2ps->stream, "trailer\n" - "<<\n" - "/Size %d\n" - "/Info 1 0 R\n" - "/Root 2 0 R\n" - ">>\n" - "startxref\n%d\n" - "%%%%EOF\n", gl2ps->objects_stack, - gl2ps->xreflist[gl2ps->objects_stack]); + "<<\n" + "/Size %d\n" + "/Info 1 0 R\n" + "/Root 2 0 R\n" + ">>\n" + "startxref\n%d\n" + "%%%%EOF\n", + gl2ps->objects_stack, gl2ps->xreflist[gl2ps->objects_stack]) ; /* Free auxiliary lists and arrays */ - gl2psFree(gl2ps->xreflist); - gl2psListAction(gl2ps->pdfprimlist, gl2psFreePrimitive); - gl2psListDelete(gl2ps->pdfprimlist); - gl2psPDFgroupListDelete(); + gl2psFree(gl2ps->xreflist) ; + gl2psListAction(gl2ps->pdfprimlist, gl2psFreePrimitive) ; + gl2psListDelete(gl2ps->pdfprimlist) ; + gl2psPDFgroupListDelete() ; #if defined(GL2PS_HAVE_ZLIB) - if(gl2ps->options & GL2PS_COMPRESS) { - gl2psFreeCompress(); - gl2psFree(gl2ps->compress); - gl2ps->compress = NULL; + if(gl2ps->options & GL2PS_COMPRESS) + { + gl2psFreeCompress() ; + gl2psFree(gl2ps->compress) ; + gl2ps->compress = NULL ; } #endif } /* PDF begin viewport */ -static void gl2psPrintPDFBeginViewport(GLint viewport[4]) { - int offs = 0; - GLint index; - GLfloat rgba[4]; - int x = viewport[0], y = viewport[1], w = viewport[2], h = viewport[3]; +static void gl2psPrintPDFBeginViewport(GLint viewport[4]) +{ + int offs = 0 ; + GLint index ; + GLfloat rgba[4] ; + int x = viewport[0], y = viewport[1], w = viewport[2], h = viewport[3] ; - glRenderMode(GL_FEEDBACK); + glRenderMode(GL_FEEDBACK) ; - if (gl2ps->header) { - gl2psPrintPDFHeader(); - gl2ps->header = GL_FALSE; + if (gl2ps->header) + { + gl2psPrintPDFHeader() ; + gl2ps->header = GL_FALSE ; } - offs += gl2psPrintf("q\n"); + offs += gl2psPrintf("q\n") ; - if (gl2ps->options & GL2PS_DRAW_BACKGROUND) { - if (gl2ps->colormode == GL_RGBA || gl2ps->colorsize == 0) { - glGetFloatv(GL_COLOR_CLEAR_VALUE, rgba); - } else { - glGetIntegerv(GL_INDEX_CLEAR_VALUE, &index); - rgba[0] = gl2ps->colormap[index][0]; - rgba[1] = gl2ps->colormap[index][1]; - rgba[2] = gl2ps->colormap[index][2]; - rgba[3] = 1.0F; + if (gl2ps->options & GL2PS_DRAW_BACKGROUND) + { + if (gl2ps->colormode == GL_RGBA || gl2ps->colorsize == 0) + { + glGetFloatv(GL_COLOR_CLEAR_VALUE, rgba) ; + } + else + { + glGetIntegerv(GL_INDEX_CLEAR_VALUE, &index) ; + rgba[0] = gl2ps->colormap[index][0] ; + rgba[1] = gl2ps->colormap[index][1] ; + rgba[2] = gl2ps->colormap[index][2] ; + rgba[3] = 1.0F ; } - offs += gl2psPrintPDFFillColor(rgba); + offs += gl2psPrintPDFFillColor(rgba) ; offs += gl2psPrintf("%d %d %d %d re\n" - "W\n" - "f\n", x, y, w, h); - } else { + "W\n" + "f\n", + x, y, w, h) ; + } + else + { offs += gl2psPrintf("%d %d %d %d re\n" - "W\n" - "n\n", x, y, w, h); + "W\n" + "n\n", + x, y, w, h) ; } - gl2ps->streamlength += offs; + gl2ps->streamlength += offs ; } -static GLint gl2psPrintPDFEndViewport(void) { - GLint res; +static GLint gl2psPrintPDFEndViewport(void) +{ + GLint res ; - res = gl2psPrintPrimitives(); - gl2ps->streamlength += gl2psPrintf("Q\n"); - return res; + res = gl2psPrintPrimitives() ; + gl2ps->streamlength += gl2psPrintf("Q\n") ; + return res ; } -static void gl2psPrintPDFFinalPrimitive(void) { +static void gl2psPrintPDFFinalPrimitive(void) +{ } /* definition of the PDF backend */ static GL2PSbackend gl2psPDF = { gl2psPrintPDFHeader, gl2psPrintPDFFooter, - gl2psPrintPDFBeginViewport, gl2psPrintPDFEndViewport, - gl2psPrintPDFPrimitive, gl2psPrintPDFFinalPrimitive, "pdf", - "Portable Document Format" }; + gl2psPrintPDFBeginViewport, gl2psPrintPDFEndViewport, + gl2psPrintPDFPrimitive, gl2psPrintPDFFinalPrimitive, "pdf", + "Portable Document Format" } ; /********************************************************************* * @@ -4682,181 +5063,199 @@ static GL2PSbackend gl2psPDF = { gl2psPrintPDFHeader, gl2psPrintPDFFooter, * *********************************************************************/ -static void gl2psSVGGetCoordsAndColors(int n, GL2PSvertex *verts, GL2PSxyz *xyz, - GL2PSrgba *rgba) { - int i, j; +static void gl2psSVGGetCoordsAndColors(int n, GL2PSvertex *verts, GL2PSxyz *xyz, GL2PSrgba *rgba) +{ + int i, j ; - for (i = 0; i < n; i++) { - xyz[i][0] = verts[i].xyz[0]; - xyz[i][1] = gl2ps->viewport[3] - verts[i].xyz[1]; - xyz[i][2] = 0.0F; - for (j = 0; j < 4; j++) - rgba[i][j] = verts[i].rgba[j]; + for (i = 0 ; i < n; i++) + { + xyz[i][0] = verts[i].xyz[0] ; + xyz[i][1] = gl2ps->viewport[3] - verts[i].xyz[1] ; + xyz[i][2] = 0.0F ; + for (j = 0 ; j < 4; j++) + rgba[i][j] = verts[i].rgba[j] ; } } -static void gl2psSVGGetColorString(GL2PSrgba rgba, char str[32]) { - int r = (int) (255. * rgba[0]); - int g = (int) (255. * rgba[1]); - int b = (int) (255. * rgba[2]); - int rc = (r < 0) ? 0 : (r > 255) ? 255 : r; - int gc = (g < 0) ? 0 : (g > 255) ? 255 : g; - int bc = (b < 0) ? 0 : (b > 255) ? 255 : b; - sprintf(str, "#%2.2x%2.2x%2.2x", rc, gc, bc); +static void gl2psSVGGetColorString(GL2PSrgba rgba, char str[32]) +{ + int r = (int)(255. * rgba[0]) ; + int g = (int)(255. * rgba[1]) ; + int b = (int)(255. * rgba[2]) ; + int rc = (r < 0) ? 0 : (r > 255) ? 255 : r ; + int gc = (g < 0) ? 0 : (g > 255) ? 255 : g ; + int bc = (b < 0) ? 0 : (b > 255) ? 255 : b ; + sprintf(str, "#%2.2x%2.2x%2.2x", rc, gc, bc) ; } -static void gl2psPrintSVGHeader(void) { - int x, y, width, height; - char col[32]; - time_t now; +static void gl2psPrintSVGHeader(void) +{ + int x, y, width, height ; + char col[32] ; + time_t now ; - time(&now); + time(&now) ; - if (gl2ps->options & GL2PS_LANDSCAPE) { - x = (int) gl2ps->viewport[1]; - y = (int) gl2ps->viewport[0]; - width = (int) gl2ps->viewport[3]; - height = (int) gl2ps->viewport[2]; - } else { - x = (int) gl2ps->viewport[0]; - y = (int) gl2ps->viewport[1]; - width = (int) gl2ps->viewport[2]; - height = (int) gl2ps->viewport[3]; + if (gl2ps->options & GL2PS_LANDSCAPE) + { + x = (int)gl2ps->viewport[1] ; + y = (int)gl2ps->viewport[0] ; + width = (int)gl2ps->viewport[3] ; + height = (int)gl2ps->viewport[2] ; + } + else + { + x = (int)gl2ps->viewport[0] ; + y = (int)gl2ps->viewport[1] ; + width = (int)gl2ps->viewport[2] ; + height = (int)gl2ps->viewport[3] ; } /* Compressed SVG files (.svgz) are simply gzipped SVG files */ - gl2psPrintGzipHeader(); + gl2psPrintGzipHeader() ; - gl2psPrintf( - "\n"); - gl2psPrintf("\n") ; + gl2psPrintf("\n", - width, height, x, y, width, height); - gl2psPrintf("%s\n", gl2ps->title); - gl2psPrintf("\n"); + " width=\"%dpx\" height=\"%dpx\" viewBox=\"%d %d %d %d\">\n", + width, height, x, y, width, height) ; + gl2psPrintf("%s\n", gl2ps->title) ; + gl2psPrintf("\n") ; gl2psPrintf("Creator: GL2PS %d.%d.%d%s, %s\n" - "For: %s\n" - "CreationDate: %s", GL2PS_MAJOR_VERSION, GL2PS_MINOR_VERSION, - GL2PS_PATCH_VERSION, GL2PS_EXTRA_VERSION, GL2PS_COPYRIGHT, - gl2ps->producer, ctime(&now)); - gl2psPrintf("\n"); - gl2psPrintf("\n"); - gl2psPrintf("\n"); - - if (gl2ps->options & GL2PS_DRAW_BACKGROUND) { - gl2psSVGGetColorString(gl2ps->bgcolor, col); - gl2psPrintf( - "\n", - col, (int) gl2ps->viewport[0], (int) gl2ps->viewport[1], - (int) gl2ps->viewport[2], (int) gl2ps->viewport[1], - (int) gl2ps->viewport[2], (int) gl2ps->viewport[3], - (int) gl2ps->viewport[0], (int) gl2ps->viewport[3]); + "For: %s\n" + "CreationDate: %s", + GL2PS_MAJOR_VERSION, GL2PS_MINOR_VERSION, GL2PS_PATCH_VERSION, GL2PS_EXTRA_VERSION, + GL2PS_COPYRIGHT, gl2ps->producer, ctime(&now)) ; + gl2psPrintf("\n") ; + gl2psPrintf("\n") ; + gl2psPrintf("\n") ; + + if (gl2ps->options & GL2PS_DRAW_BACKGROUND) + { + gl2psSVGGetColorString(gl2ps->bgcolor, col) ; + gl2psPrintf("\n", col, + (int)gl2ps->viewport[0], (int)gl2ps->viewport[1], (int)gl2ps->viewport[2], + (int)gl2ps->viewport[1], (int)gl2ps->viewport[2], (int)gl2ps->viewport[3], + (int)gl2ps->viewport[0], (int)gl2ps->viewport[3]) ; } /* group all the primitives and disable antialiasing */ - gl2psPrintf("\n"); + gl2psPrintf("\n") ; } -static void gl2psPrintSVGSmoothTriangle(GL2PSxyz xyz[3], GL2PSrgba rgba[3]) { - int i; - GL2PSxyz xyz2[3]; - GL2PSrgba rgba2[3]; - char col[32]; +static void gl2psPrintSVGSmoothTriangle(GL2PSxyz xyz[3], GL2PSrgba rgba[3]) +{ + int i ; + GL2PSxyz xyz2[3] ; + GL2PSrgba rgba2[3] ; + char col[32] ; /* Apparently there is no easy way to do Gouraud shading in SVG without explicitly pre-defining gradients, so for now we just do recursive subdivision */ - if (gl2psSameColorThreshold(3, rgba, gl2ps->threshold)) { - gl2psSVGGetColorString(rgba[0], col); - gl2psPrintf("\n", xyz[0][0], xyz[0][1], - xyz[1][0], xyz[1][1], xyz[2][0], xyz[2][1]); - } else { + if (gl2psSameColorThreshold(3, rgba, gl2ps->threshold)) + { + gl2psSVGGetColorString(rgba[0], col) ; + gl2psPrintf("\n", xyz[0][0], xyz[0][1], xyz[1][0], xyz[1][1], + xyz[2][0], xyz[2][1]) ; + } + else + { /* subdivide into 4 subtriangles */ - for (i = 0; i < 3; i++) { - xyz2[0][i] = xyz[0][i]; - xyz2[1][i] = 0.5F * (xyz[0][i] + xyz[1][i]); - xyz2[2][i] = 0.5F * (xyz[0][i] + xyz[2][i]); + for (i = 0 ; i < 3; i++) + { + xyz2[0][i] = xyz[0][i] ; + xyz2[1][i] = 0.5F * (xyz[0][i] + xyz[1][i]) ; + xyz2[2][i] = 0.5F * (xyz[0][i] + xyz[2][i]) ; } - for (i = 0; i < 4; i++) { - rgba2[0][i] = rgba[0][i]; - rgba2[1][i] = 0.5F * (rgba[0][i] + rgba[1][i]); - rgba2[2][i] = 0.5F * (rgba[0][i] + rgba[2][i]); + for (i = 0 ; i < 4; i++) + { + rgba2[0][i] = rgba[0][i] ; + rgba2[1][i] = 0.5F * (rgba[0][i] + rgba[1][i]) ; + rgba2[2][i] = 0.5F * (rgba[0][i] + rgba[2][i]) ; } - gl2psPrintSVGSmoothTriangle(xyz2, rgba2); - for (i = 0; i < 3; i++) { - xyz2[0][i] = 0.5F * (xyz[0][i] + xyz[1][i]); - xyz2[1][i] = xyz[1][i]; - xyz2[2][i] = 0.5F * (xyz[1][i] + xyz[2][i]); + gl2psPrintSVGSmoothTriangle(xyz2, rgba2) ; + for (i = 0 ; i < 3; i++) + { + xyz2[0][i] = 0.5F * (xyz[0][i] + xyz[1][i]) ; + xyz2[1][i] = xyz[1][i] ; + xyz2[2][i] = 0.5F * (xyz[1][i] + xyz[2][i]) ; } - for (i = 0; i < 4; i++) { - rgba2[0][i] = 0.5F * (rgba[0][i] + rgba[1][i]); - rgba2[1][i] = rgba[1][i]; - rgba2[2][i] = 0.5F * (rgba[1][i] + rgba[2][i]); + for (i = 0 ; i < 4; i++) + { + rgba2[0][i] = 0.5F * (rgba[0][i] + rgba[1][i]) ; + rgba2[1][i] = rgba[1][i] ; + rgba2[2][i] = 0.5F * (rgba[1][i] + rgba[2][i]) ; } - gl2psPrintSVGSmoothTriangle(xyz2, rgba2); - for (i = 0; i < 3; i++) { - xyz2[0][i] = 0.5F * (xyz[0][i] + xyz[2][i]); - xyz2[1][i] = xyz[2][i]; - xyz2[2][i] = 0.5F * (xyz[1][i] + xyz[2][i]); + gl2psPrintSVGSmoothTriangle(xyz2, rgba2) ; + for (i = 0 ; i < 3; i++) + { + xyz2[0][i] = 0.5F * (xyz[0][i] + xyz[2][i]) ; + xyz2[1][i] = xyz[2][i] ; + xyz2[2][i] = 0.5F * (xyz[1][i] + xyz[2][i]) ; } - for (i = 0; i < 4; i++) { - rgba2[0][i] = 0.5F * (rgba[0][i] + rgba[2][i]); - rgba2[1][i] = rgba[2][i]; - rgba2[2][i] = 0.5F * (rgba[1][i] + rgba[2][i]); + for (i = 0 ; i < 4; i++) + { + rgba2[0][i] = 0.5F * (rgba[0][i] + rgba[2][i]) ; + rgba2[1][i] = rgba[2][i] ; + rgba2[2][i] = 0.5F * (rgba[1][i] + rgba[2][i]) ; } - gl2psPrintSVGSmoothTriangle(xyz2, rgba2); - for (i = 0; i < 3; i++) { - xyz2[0][i] = 0.5F * (xyz[0][i] + xyz[1][i]); - xyz2[1][i] = 0.5F * (xyz[1][i] + xyz[2][i]); - xyz2[2][i] = 0.5F * (xyz[0][i] + xyz[2][i]); + gl2psPrintSVGSmoothTriangle(xyz2, rgba2) ; + for (i = 0 ; i < 3; i++) + { + xyz2[0][i] = 0.5F * (xyz[0][i] + xyz[1][i]) ; + xyz2[1][i] = 0.5F * (xyz[1][i] + xyz[2][i]) ; + xyz2[2][i] = 0.5F * (xyz[0][i] + xyz[2][i]) ; } - for (i = 0; i < 4; i++) { - rgba2[0][i] = 0.5F * (rgba[0][i] + rgba[1][i]); - rgba2[1][i] = 0.5F * (rgba[1][i] + rgba[2][i]); - rgba2[2][i] = 0.5F * (rgba[0][i] + rgba[2][i]); + for (i = 0 ; i < 4; i++) + { + rgba2[0][i] = 0.5F * (rgba[0][i] + rgba[1][i]) ; + rgba2[1][i] = 0.5F * (rgba[1][i] + rgba[2][i]) ; + rgba2[2][i] = 0.5F * (rgba[0][i] + rgba[2][i]) ; } - gl2psPrintSVGSmoothTriangle(xyz2, rgba2); + gl2psPrintSVGSmoothTriangle(xyz2, rgba2) ; } } -static void gl2psPrintSVGDash(GLushort pattern, GLint factor) { - int i, n, array[10]; +static void gl2psPrintSVGDash(GLushort pattern, GLint factor) +{ + int i, n, array[10] ; - if (!pattern || !factor) - return; /* solid line */ + if (!pattern || !factor) return ; /* solid line */ - gl2psParseStipplePattern(pattern, factor, &n, array); - gl2psPrintf("stroke-dasharray=\""); - for (i = 0; i < n; i++) { - if (i) - gl2psPrintf(","); - gl2psPrintf("%d", array[i]); + gl2psParseStipplePattern(pattern, factor, &n, array) ; + gl2psPrintf("stroke-dasharray=\"") ; + for (i = 0 ; i < n; i++) + { + if (i) gl2psPrintf(",") ; + gl2psPrintf("%d", array[i]) ; } - gl2psPrintf("\" "); + gl2psPrintf("\" ") ; } -static void gl2psEndSVGLine(void) { - int i; - if (gl2ps->lastvertex.rgba[0] >= 0.) { +static void gl2psEndSVGLine(void) +{ + int i ; + if (gl2ps->lastvertex.rgba[0] >= 0.) + { gl2psPrintf("%g,%g\"/>\n", gl2ps->lastvertex.xyz[0], - gl2ps->viewport[3] - gl2ps->lastvertex.xyz[1]); - for (i = 0; i < 3; i++) - gl2ps->lastvertex.xyz[i] = -1.; - for (i = 0; i < 4; i++) - gl2ps->lastvertex.rgba[i] = -1.; + gl2ps->viewport[3] - gl2ps->lastvertex.xyz[1]) ; + for (i = 0 ; i < 3; i++) + gl2ps->lastvertex.xyz[i] = -1. ; + for (i = 0 ; i < 4; i++) + gl2ps->lastvertex.rgba[i] = -1. ; } } -static void gl2psPrintSVGPixmap(GLfloat x, GLfloat y, GL2PSimage *pixmap) { +static void gl2psPrintSVGPixmap(GLfloat x, GLfloat y, GL2PSimage *pixmap) +{ #if defined(GL2PS_HAVE_LIBPNG) - GL2PSlist *png; - unsigned char c; - int i; + GL2PSlist *png ; + unsigned char c ; + int i ; /* The only image types supported by the SVG standard are JPEG, PNG and SVG. Here we choose PNG, and since we want to embed the image @@ -4865,205 +5264,209 @@ static void gl2psPrintSVGPixmap(GLfloat x, GLfloat y, GL2PSimage *pixmap) { encode it into base64. */ png = gl2psListCreate(pixmap->width * pixmap->height * 3, 1000, - sizeof(unsigned char)); - gl2psConvertPixmapToPNG(pixmap, png); - gl2psListEncodeBase64(png); + sizeof(unsigned char)) ; + gl2psConvertPixmapToPNG(pixmap, png) ; + gl2psListEncodeBase64(png) ; gl2psPrintf("height, pixmap->width, pixmap->height); - gl2psPrintf("xlink:href=\"data:image/png;base64,"); - for(i = 0; i < gl2psListNbr(png); i++) { - gl2psListRead(png, i, &c); - gl2psPrintf("%c", c); - } - gl2psPrintf("\"/>\n"); - gl2psListDelete(png); + x, y - pixmap->height, pixmap->width, pixmap->height) ; + gl2psPrintf("xlink:href=\"data:image/png;base64,") ; + for(i = 0 ; i < gl2psListNbr(png) ; i++) + { + gl2psListRead(png, i, &c) ; + gl2psPrintf("%c", c) ; + } + gl2psPrintf("\"/>\n") ; + gl2psListDelete(png) ; #else gl2psMsg(GL2PS_WARNING, "GL2PS must be compiled with PNG support in " - "order to embed images in SVG streams"); + "order to embed images in SVG streams") ; #endif } -static void gl2psPrintSVGPrimitive(void *data) { - GL2PSprimitive *prim; - GL2PSxyz xyz[4]; - GL2PSrgba rgba[4]; - char col[32]; - int newline; +static void gl2psPrintSVGPrimitive(void *data) +{ + GL2PSprimitive *prim ; + GL2PSxyz xyz[4] ; + GL2PSrgba rgba[4] ; + char col[32] ; + int newline ; - prim = *(GL2PSprimitive**) data; + prim = *(GL2PSprimitive**)data ; - if ((gl2ps->options & GL2PS_OCCLUSION_CULL) && prim->culled) - return; + if ((gl2ps->options & GL2PS_OCCLUSION_CULL) && prim->culled) return ; /* We try to draw connected lines as a single path to get nice line joins and correct stippling. So if the primitive to print is not a line we must first finish the current line (if any): */ - if (prim->type != GL2PS_LINE) - gl2psEndSVGLine(); - - gl2psSVGGetCoordsAndColors(prim->numverts, prim->verts, xyz, rgba); - - switch (prim->type) { - case GL2PS_POINT: - gl2psSVGGetColorString(rgba[0], col); - gl2psPrintf("\n", xyz[0][0], xyz[0][1], - 0.5 * prim->width); - break; - case GL2PS_LINE: + if (prim->type != GL2PS_LINE) gl2psEndSVGLine() ; + + gl2psSVGGetCoordsAndColors(prim->numverts, prim->verts, xyz, rgba) ; + + switch (prim->type) + { + case GL2PS_POINT : + gl2psSVGGetColorString(rgba[0], col) ; + gl2psPrintf("\n", xyz[0][0], xyz[0][1], 0.5 * prim->width) ; + break ; + case GL2PS_LINE : if (!gl2psSamePosition(gl2ps->lastvertex.xyz, prim->verts[0].xyz) - || !gl2psSameColor(gl2ps->lastrgba, prim->verts[0].rgba) - || gl2ps->lastlinewidth != prim->width - || gl2ps->lastpattern != prim->pattern - || gl2ps->lastfactor != prim->factor) { + || !gl2psSameColor(gl2ps->lastrgba, prim->verts[0].rgba) + || gl2ps->lastlinewidth != prim->width || gl2ps->lastpattern != prim->pattern + || gl2ps->lastfactor != prim->factor) + { /* End the current line if the new segment does not start where the last one ended, or if the color, the width or the stippling have changed (we will need to use multi-point gradients for smooth-shaded lines) */ - gl2psEndSVGLine(); - newline = 1; - } else { - newline = 0; + gl2psEndSVGLine() ; + newline = 1 ; + } + else + { + newline = 0 ; } - gl2ps->lastvertex = prim->verts[1]; - gl2psSetLastColor(prim->verts[0].rgba); - gl2ps->lastlinewidth = prim->width; - gl2ps->lastpattern = prim->pattern; - gl2ps->lastfactor = prim->factor; - if (newline) { - gl2psSVGGetColorString(rgba[0], col); - gl2psPrintf( - "width); - if (rgba[0][3] < 1.0F) - gl2psPrintf("stroke-opacity=\"%g\" ", rgba[0][3]); - gl2psPrintSVGDash(prim->pattern, prim->factor); - gl2psPrintf("points=\"%g,%g ", xyz[0][0], xyz[0][1]); - } else { - gl2psPrintf("%g,%g ", xyz[0][0], xyz[0][1]); + gl2ps->lastvertex = prim->verts[1] ; + gl2psSetLastColor(prim->verts[0].rgba) ; + gl2ps->lastlinewidth = prim->width ; + gl2ps->lastpattern = prim->pattern ; + gl2ps->lastfactor = prim->factor ; + if (newline) + { + gl2psSVGGetColorString(rgba[0], col) ; + gl2psPrintf("width) ; + if (rgba[0][3] < 1.0F) gl2psPrintf("stroke-opacity=\"%g\" ", rgba[0][3]) ; + gl2psPrintSVGDash(prim->pattern, prim->factor) ; + gl2psPrintf("points=\"%g,%g ", xyz[0][0], xyz[0][1]) ; } - break; - case GL2PS_TRIANGLE: - gl2psPrintSVGSmoothTriangle(xyz, rgba); - break; - case GL2PS_QUADRANGLE: - gl2psMsg(GL2PS_WARNING, "There should not be any quad left to print"); - break; - case GL2PS_PIXMAP: - gl2psPrintSVGPixmap(xyz[0][0], xyz[0][1], prim->data.image); - break; - case GL2PS_TEXT: - gl2psSVGGetColorString(prim->verts[0].rgba, col); - gl2psPrintf("data.text->fontsize); - if (prim->data.text->angle) - gl2psPrintf("transform=\"rotate(%g, %g, %g)\" ", - -prim->data.text->angle, xyz[0][0], xyz[0][1]); + else + { + gl2psPrintf("%g,%g ", xyz[0][0], xyz[0][1]) ; + } + break ; + case GL2PS_TRIANGLE : + gl2psPrintSVGSmoothTriangle(xyz, rgba) ; + break ; + case GL2PS_QUADRANGLE : + gl2psMsg(GL2PS_WARNING, "There should not be any quad left to print") ; + break ; + case GL2PS_PIXMAP : + gl2psPrintSVGPixmap(xyz[0][0], xyz[0][1], prim->data.image) ; + break ; + case GL2PS_TEXT : + gl2psSVGGetColorString(prim->verts[0].rgba, col) ; + gl2psPrintf("data.text->fontsize) ; + if (prim->data.text->angle) gl2psPrintf("transform=\"rotate(%g, %g, %g)\" ", + -prim->data.text->angle, xyz[0][0], xyz[0][1]) ; if (!strcmp(prim->data.text->fontname, "Times-Roman")) - gl2psPrintf("font-family=\"Times\">"); + gl2psPrintf("font-family=\"Times\">") ; else if (!strcmp(prim->data.text->fontname, "Times-Bold")) - gl2psPrintf("font-family=\"Times\" font-weight=\"bold\">"); + gl2psPrintf("font-family=\"Times\" font-weight=\"bold\">") ; else if (!strcmp(prim->data.text->fontname, "Times-Italic")) - gl2psPrintf("font-family=\"Times\" font-style=\"italic\">"); + gl2psPrintf("font-family=\"Times\" font-style=\"italic\">") ; else if (!strcmp(prim->data.text->fontname, "Times-BoldItalic")) - gl2psPrintf( - "font-family=\"Times\" font-style=\"italic\" font-weight=\"bold\">"); + gl2psPrintf("font-family=\"Times\" font-style=\"italic\" font-weight=\"bold\">") ; else if (!strcmp(prim->data.text->fontname, "Helvetica-Bold")) - gl2psPrintf("font-family=\"Helvetica\" font-weight=\"bold\">"); + gl2psPrintf("font-family=\"Helvetica\" font-weight=\"bold\">") ; else if (!strcmp(prim->data.text->fontname, "Helvetica-Oblique")) - gl2psPrintf("font-family=\"Helvetica\" font-style=\"oblique\">"); + gl2psPrintf("font-family=\"Helvetica\" font-style=\"oblique\">") ; else if (!strcmp(prim->data.text->fontname, "Helvetica-BoldOblique")) - gl2psPrintf( - "font-family=\"Helvetica\" font-style=\"oblique\" font-weight=\"bold\">"); + gl2psPrintf("font-family=\"Helvetica\" font-style=\"oblique\" font-weight=\"bold\">") ; else if (!strcmp(prim->data.text->fontname, "Courier-Bold")) - gl2psPrintf("font-family=\"Courier\" font-weight=\"bold\">"); + gl2psPrintf("font-family=\"Courier\" font-weight=\"bold\">") ; else if (!strcmp(prim->data.text->fontname, "Courier-Oblique")) - gl2psPrintf("font-family=\"Courier\" font-style=\"oblique\">"); + gl2psPrintf("font-family=\"Courier\" font-style=\"oblique\">") ; else if (!strcmp(prim->data.text->fontname, "Courier-BoldOblique")) - gl2psPrintf( - "font-family=\"Courier\" font-style=\"oblique\" font-weight=\"bold\">"); + gl2psPrintf("font-family=\"Courier\" font-style=\"oblique\" font-weight=\"bold\">") ; else - gl2psPrintf("font-family=\"%s\">", prim->data.text->fontname); - gl2psPrintf("%s\n", prim->data.text->str); - break; - case GL2PS_SPECIAL: + gl2psPrintf("font-family=\"%s\">", prim->data.text->fontname) ; + gl2psPrintf("%s\n", prim->data.text->str) ; + break ; + case GL2PS_SPECIAL : /* alignment contains the format for which the special output text is intended */ - if (prim->data.text->alignment == GL2PS_SVG) - gl2psPrintf("%s\n", prim->data.text->str); - break; - default: - break; + if (prim->data.text->alignment == GL2PS_SVG) gl2psPrintf("%s\n", prim->data.text->str) ; + break ; + default : + break ; } } -static void gl2psPrintSVGFooter(void) { - gl2psPrintf("\n"); - gl2psPrintf("\n"); +static void gl2psPrintSVGFooter(void) +{ + gl2psPrintf("\n") ; + gl2psPrintf("\n") ; - gl2psPrintGzipFooter(); + gl2psPrintGzipFooter() ; } -static void gl2psPrintSVGBeginViewport(GLint viewport[4]) { - GLint index; - char col[32]; - GLfloat rgba[4]; - int x = viewport[0], y = viewport[1], w = viewport[2], h = viewport[3]; +static void gl2psPrintSVGBeginViewport(GLint viewport[4]) +{ + GLint index ; + char col[32] ; + GLfloat rgba[4] ; + int x = viewport[0], y = viewport[1], w = viewport[2], h = viewport[3] ; - glRenderMode(GL_FEEDBACK); + glRenderMode(GL_FEEDBACK) ; - if (gl2ps->header) { - gl2psPrintSVGHeader(); - gl2ps->header = GL_FALSE; + if (gl2ps->header) + { + gl2psPrintSVGHeader() ; + gl2ps->header = GL_FALSE ; } - if (gl2ps->options & GL2PS_DRAW_BACKGROUND) { - if (gl2ps->colormode == GL_RGBA || gl2ps->colorsize == 0) { - glGetFloatv(GL_COLOR_CLEAR_VALUE, rgba); - } else { - glGetIntegerv(GL_INDEX_CLEAR_VALUE, &index); - rgba[0] = gl2ps->colormap[index][0]; - rgba[1] = gl2ps->colormap[index][1]; - rgba[2] = gl2ps->colormap[index][2]; - rgba[3] = 1.0F; + if (gl2ps->options & GL2PS_DRAW_BACKGROUND) + { + if (gl2ps->colormode == GL_RGBA || gl2ps->colorsize == 0) + { + glGetFloatv(GL_COLOR_CLEAR_VALUE, rgba) ; } - gl2psSVGGetColorString(rgba, col); - gl2psPrintf( - "\n", - col, x, gl2ps->viewport[3] - y, x + w, gl2ps->viewport[3] - y, - x + w, gl2ps->viewport[3] - (y + h), x, - gl2ps->viewport[3] - (y + h)); + else + { + glGetIntegerv(GL_INDEX_CLEAR_VALUE, &index) ; + rgba[0] = gl2ps->colormap[index][0] ; + rgba[1] = gl2ps->colormap[index][1] ; + rgba[2] = gl2ps->colormap[index][2] ; + rgba[3] = 1.0F ; + } + gl2psSVGGetColorString(rgba, col) ; + gl2psPrintf("\n", col, x, + gl2ps->viewport[3] - y, x + w, gl2ps->viewport[3] - y, x + w, + gl2ps->viewport[3] - (y + h), x, gl2ps->viewport[3] - (y + h)) ; } - gl2psPrintf("\n", x, y, w, h); - gl2psPrintf(" \n", x, - gl2ps->viewport[3] - y, x + w, gl2ps->viewport[3] - y, x + w, - gl2ps->viewport[3] - (y + h), x, gl2ps->viewport[3] - (y + h)); - gl2psPrintf("\n"); - gl2psPrintf("\n", x, y, w, h); + gl2psPrintf("\n", x, y, w, h) ; + gl2psPrintf(" \n", x, gl2ps->viewport[3] - y, + x + w, gl2ps->viewport[3] - y, x + w, gl2ps->viewport[3] - (y + h), x, + gl2ps->viewport[3] - (y + h)) ; + gl2psPrintf("\n") ; + gl2psPrintf("\n", x, y, w, h) ; } -static GLint gl2psPrintSVGEndViewport(void) { - GLint res; +static GLint gl2psPrintSVGEndViewport(void) +{ + GLint res ; - res = gl2psPrintPrimitives(); - gl2psPrintf("\n"); - return res; + res = gl2psPrintPrimitives() ; + gl2psPrintf("\n") ; + return res ; } -static void gl2psPrintSVGFinalPrimitive(void) { +static void gl2psPrintSVGFinalPrimitive(void) +{ /* End any remaining line, if any */ - gl2psEndSVGLine(); + gl2psEndSVGLine() ; } /* definition of the SVG backend */ static GL2PSbackend gl2psSVG = { gl2psPrintSVGHeader, gl2psPrintSVGFooter, - gl2psPrintSVGBeginViewport, gl2psPrintSVGEndViewport, - gl2psPrintSVGPrimitive, gl2psPrintSVGFinalPrimitive, "svg", - "Scalable Vector Graphics" }; + gl2psPrintSVGBeginViewport, gl2psPrintSVGEndViewport, + gl2psPrintSVGPrimitive, gl2psPrintSVGFinalPrimitive, "svg", + "Scalable Vector Graphics" } ; /********************************************************************* * @@ -5071,211 +5474,226 @@ static GL2PSbackend gl2psSVG = { gl2psPrintSVGHeader, gl2psPrintSVGFooter, * *********************************************************************/ -static void gl2psPrintPGFColor(GL2PSrgba rgba) { - if (!gl2psSameColor(gl2ps->lastrgba, rgba)) { - gl2psSetLastColor(rgba); - fprintf(gl2ps->stream, "\\color[rgb]{%f,%f,%f}\n", rgba[0], rgba[1], - rgba[2]); +static void gl2psPrintPGFColor(GL2PSrgba rgba) +{ + if (!gl2psSameColor(gl2ps->lastrgba, rgba)) + { + gl2psSetLastColor(rgba) ; + fprintf(gl2ps->stream, "\\color[rgb]{%f,%f,%f}\n", rgba[0], rgba[1], rgba[2]) ; } } -static void gl2psPrintPGFHeader(void) { - time_t now; +static void gl2psPrintPGFHeader(void) +{ + time_t now ; - time(&now); + time(&now) ; fprintf(gl2ps->stream, "%% Title: %s\n" - "%% Creator: GL2PS %d.%d.%d%s, %s\n" - "%% For: %s\n" - "%% CreationDate: %s", gl2ps->title, GL2PS_MAJOR_VERSION, - GL2PS_MINOR_VERSION, GL2PS_PATCH_VERSION, GL2PS_EXTRA_VERSION, - GL2PS_COPYRIGHT, gl2ps->producer, ctime(&now)); - - fprintf(gl2ps->stream, "\\begin{pgfpicture}\n"); - if (gl2ps->options & GL2PS_DRAW_BACKGROUND) { - gl2psPrintPGFColor(gl2ps->bgcolor); + "%% Creator: GL2PS %d.%d.%d%s, %s\n" + "%% For: %s\n" + "%% CreationDate: %s", + gl2ps->title, GL2PS_MAJOR_VERSION, GL2PS_MINOR_VERSION, GL2PS_PATCH_VERSION, + GL2PS_EXTRA_VERSION, GL2PS_COPYRIGHT, gl2ps->producer, ctime(&now)) ; + + fprintf(gl2ps->stream, "\\begin{pgfpicture}\n") ; + if (gl2ps->options & GL2PS_DRAW_BACKGROUND) + { + gl2psPrintPGFColor(gl2ps->bgcolor) ; fprintf(gl2ps->stream, "\\pgfpathrectanglecorners{" - "\\pgfpoint{%dpt}{%dpt}}{\\pgfpoint{%dpt}{%dpt}}\n" - "\\pgfusepath{fill}\n", (int) gl2ps->viewport[0], - (int) gl2ps->viewport[1], (int) gl2ps->viewport[2], - (int) gl2ps->viewport[3]); + "\\pgfpoint{%dpt}{%dpt}}{\\pgfpoint{%dpt}{%dpt}}\n" + "\\pgfusepath{fill}\n", + (int)gl2ps->viewport[0], (int)gl2ps->viewport[1], (int)gl2ps->viewport[2], + (int)gl2ps->viewport[3]) ; } } -static void gl2psPrintPGFDash(GLushort pattern, GLint factor) { - int i, n, array[10]; +static void gl2psPrintPGFDash(GLushort pattern, GLint factor) +{ + int i, n, array[10] ; - if (pattern == gl2ps->lastpattern && factor == gl2ps->lastfactor) - return; + if (pattern == gl2ps->lastpattern && factor == gl2ps->lastfactor) return ; - gl2ps->lastpattern = pattern; - gl2ps->lastfactor = factor; + gl2ps->lastpattern = pattern ; + gl2ps->lastfactor = factor ; - if (!pattern || !factor) { + if (!pattern || !factor) + { /* solid line */ - fprintf(gl2ps->stream, "\\pgfsetdash{}{0pt}\n"); - } else { - gl2psParseStipplePattern(pattern, factor, &n, array); - fprintf(gl2ps->stream, "\\pgfsetdash{"); - for (i = 0; i < n; i++) - fprintf(gl2ps->stream, "{%dpt}", array[i]); - fprintf(gl2ps->stream, "}{0pt}\n"); - } -} - -static const char *gl2psPGFTextAlignment(int align) { - switch (align) { - case GL2PS_TEXT_C: - return "center"; - case GL2PS_TEXT_CL: - return "west"; - case GL2PS_TEXT_CR: - return "east"; - case GL2PS_TEXT_B: - return "south"; - case GL2PS_TEXT_BR: - return "south east"; - case GL2PS_TEXT_T: - return "north"; - case GL2PS_TEXT_TL: - return "north west"; - case GL2PS_TEXT_TR: - return "north east"; - case GL2PS_TEXT_BL: - default: - return "south west"; - } -} - -static void gl2psPrintPGFPrimitive(void *data) { - GL2PSprimitive *prim; - - prim = *(GL2PSprimitive**) data; - - switch (prim->type) { - case GL2PS_POINT: + fprintf(gl2ps->stream, "\\pgfsetdash{}{0pt}\n") ; + } + else + { + gl2psParseStipplePattern(pattern, factor, &n, array) ; + fprintf(gl2ps->stream, "\\pgfsetdash{") ; + for (i = 0 ; i < n; i++) + fprintf(gl2ps->stream, "{%dpt}", array[i]) ; + fprintf(gl2ps->stream, "}{0pt}\n") ; + } +} + +static const char *gl2psPGFTextAlignment(int align) +{ + switch (align) + { + case GL2PS_TEXT_C : + return "center" ; + case GL2PS_TEXT_CL : + return "west" ; + case GL2PS_TEXT_CR : + return "east" ; + case GL2PS_TEXT_B : + return "south" ; + case GL2PS_TEXT_BR : + return "south east" ; + case GL2PS_TEXT_T : + return "north" ; + case GL2PS_TEXT_TL : + return "north west" ; + case GL2PS_TEXT_TR : + return "north east" ; + case GL2PS_TEXT_BL : + default : + return "south west" ; + } +} + +static void gl2psPrintPGFPrimitive(void *data) +{ + GL2PSprimitive *prim ; + + prim = *(GL2PSprimitive**)data ; + + switch (prim->type) + { + case GL2PS_POINT : /* Points in openGL are rectangular */ - gl2psPrintPGFColor(prim->verts[0].rgba); + gl2psPrintPGFColor(prim->verts[0].rgba) ; fprintf(gl2ps->stream, "\\pgfpathrectangle{\\pgfpoint{%fpt}{%fpt}}" - "{\\pgfpoint{%fpt}{%fpt}}\n\\pgfusepath{fill}\n", - prim->verts[0].xyz[0] - 0.5 * prim->width, - prim->verts[0].xyz[1] - 0.5 * prim->width, prim->width, - prim->width); - break; - case GL2PS_LINE: - gl2psPrintPGFColor(prim->verts[0].rgba); - if (gl2ps->lastlinewidth != prim->width) { - gl2ps->lastlinewidth = prim->width; - fprintf(gl2ps->stream, "\\pgfsetlinewidth{%fpt}\n", - gl2ps->lastlinewidth); + "{\\pgfpoint{%fpt}{%fpt}}\n\\pgfusepath{fill}\n", + prim->verts[0].xyz[0] - 0.5 * prim->width, + prim->verts[0].xyz[1] - 0.5 * prim->width, prim->width, prim->width) ; + break ; + case GL2PS_LINE : + gl2psPrintPGFColor(prim->verts[0].rgba) ; + if (gl2ps->lastlinewidth != prim->width) + { + gl2ps->lastlinewidth = prim->width ; + fprintf(gl2ps->stream, "\\pgfsetlinewidth{%fpt}\n", gl2ps->lastlinewidth) ; } - gl2psPrintPGFDash(prim->pattern, prim->factor); + gl2psPrintPGFDash(prim->pattern, prim->factor) ; fprintf(gl2ps->stream, "\\pgfpathmoveto{\\pgfpoint{%fpt}{%fpt}}\n" - "\\pgflineto{\\pgfpoint{%fpt}{%fpt}}\n" - "\\pgfusepath{stroke}\n", prim->verts[1].xyz[0], - prim->verts[1].xyz[1], prim->verts[0].xyz[0], - prim->verts[0].xyz[1]); - break; - case GL2PS_TRIANGLE: - if (gl2ps->lastlinewidth != 0) { - gl2ps->lastlinewidth = 0; - fprintf(gl2ps->stream, "\\pgfsetlinewidth{0.01pt}\n"); + "\\pgflineto{\\pgfpoint{%fpt}{%fpt}}\n" + "\\pgfusepath{stroke}\n", + prim->verts[1].xyz[0], prim->verts[1].xyz[1], prim->verts[0].xyz[0], + prim->verts[0].xyz[1]) ; + break ; + case GL2PS_TRIANGLE : + if (gl2ps->lastlinewidth != 0) + { + gl2ps->lastlinewidth = 0 ; + fprintf(gl2ps->stream, "\\pgfsetlinewidth{0.01pt}\n") ; } - gl2psPrintPGFColor(prim->verts[0].rgba); + gl2psPrintPGFColor(prim->verts[0].rgba) ; fprintf(gl2ps->stream, "\\pgfpathmoveto{\\pgfpoint{%fpt}{%fpt}}\n" - "\\pgflineto{\\pgfpoint{%fpt}{%fpt}}\n" - "\\pgflineto{\\pgfpoint{%fpt}{%fpt}}\n" - "\\pgfpathclose\n" - "\\pgfusepath{fill,stroke}\n", prim->verts[2].xyz[0], - prim->verts[2].xyz[1], prim->verts[1].xyz[0], - prim->verts[1].xyz[1], prim->verts[0].xyz[0], - prim->verts[0].xyz[1]); - break; - case GL2PS_TEXT: - fprintf(gl2ps->stream, - "{\n\\pgftransformshift{\\pgfpoint{%fpt}{%fpt}}\n", - prim->verts[0].xyz[0], prim->verts[0].xyz[1]); - - if (prim->data.text->angle) - fprintf(gl2ps->stream, "\\pgftransformrotate{%f}{", - prim->data.text->angle); - - fprintf(gl2ps->stream, - "\\pgfnode{rectangle}{%s}{\\fontsize{%d}{0}\\selectfont", - gl2psPGFTextAlignment(prim->data.text->alignment), - prim->data.text->fontsize); - - fprintf(gl2ps->stream, "\\textcolor[rgb]{%g,%g,%g}{{%s}}", - prim->verts[0].rgba[0], prim->verts[0].rgba[1], - prim->verts[0].rgba[2], prim->data.text->str); - - fprintf(gl2ps->stream, "}{}{\\pgfusepath{discard}}}\n"); - break; - case GL2PS_SPECIAL: + "\\pgflineto{\\pgfpoint{%fpt}{%fpt}}\n" + "\\pgflineto{\\pgfpoint{%fpt}{%fpt}}\n" + "\\pgfpathclose\n" + "\\pgfusepath{fill,stroke}\n", + prim->verts[2].xyz[0], prim->verts[2].xyz[1], prim->verts[1].xyz[0], + prim->verts[1].xyz[1], prim->verts[0].xyz[0], prim->verts[0].xyz[1]) ; + break ; + case GL2PS_TEXT : + fprintf(gl2ps->stream, "{\n\\pgftransformshift{\\pgfpoint{%fpt}{%fpt}}\n", + prim->verts[0].xyz[0], prim->verts[0].xyz[1]) ; + + if (prim->data.text->angle) fprintf(gl2ps->stream, "\\pgftransformrotate{%f}{", + prim->data.text->angle) ; + + fprintf(gl2ps->stream, "\\pgfnode{rectangle}{%s}{\\fontsize{%d}{0}\\selectfont", + gl2psPGFTextAlignment(prim->data.text->alignment), prim->data.text->fontsize) ; + + fprintf(gl2ps->stream, "\\textcolor[rgb]{%g,%g,%g}{{%s}}", prim->verts[0].rgba[0], + prim->verts[0].rgba[1], prim->verts[0].rgba[2], prim->data.text->str) ; + + fprintf(gl2ps->stream, "}{}{\\pgfusepath{discard}}}\n") ; + break ; + case GL2PS_SPECIAL : /* alignment contains the format for which the special output text is intended */ - if (prim->data.text->alignment == GL2PS_PGF) - fprintf(gl2ps->stream, "%s\n", prim->data.text->str); - break; - default: - break; + if (prim->data.text->alignment == GL2PS_PGF) fprintf(gl2ps->stream, "%s\n", + prim->data.text->str) ; + break ; + default : + break ; } } -static void gl2psPrintPGFFooter(void) { - fprintf(gl2ps->stream, "\\end{pgfpicture}\n"); +static void gl2psPrintPGFFooter(void) +{ + fprintf(gl2ps->stream, "\\end{pgfpicture}\n") ; } -static void gl2psPrintPGFBeginViewport(GLint viewport[4]) { - GLint index; - GLfloat rgba[4]; - int x = viewport[0], y = viewport[1], w = viewport[2], h = viewport[3]; +static void gl2psPrintPGFBeginViewport(GLint viewport[4]) +{ + GLint index ; + GLfloat rgba[4] ; + int x = viewport[0], y = viewport[1], w = viewport[2], h = viewport[3] ; - glRenderMode(GL_FEEDBACK); + glRenderMode(GL_FEEDBACK) ; - if (gl2ps->header) { - gl2psPrintPGFHeader(); - gl2ps->header = GL_FALSE; + if (gl2ps->header) + { + gl2psPrintPGFHeader() ; + gl2ps->header = GL_FALSE ; } - fprintf(gl2ps->stream, "\\begin{pgfscope}\n"); - if (gl2ps->options & GL2PS_DRAW_BACKGROUND) { - if (gl2ps->colormode == GL_RGBA || gl2ps->colorsize == 0) { - glGetFloatv(GL_COLOR_CLEAR_VALUE, rgba); - } else { - glGetIntegerv(GL_INDEX_CLEAR_VALUE, &index); - rgba[0] = gl2ps->colormap[index][0]; - rgba[1] = gl2ps->colormap[index][1]; - rgba[2] = gl2ps->colormap[index][2]; - rgba[3] = 1.0F; + fprintf(gl2ps->stream, "\\begin{pgfscope}\n") ; + if (gl2ps->options & GL2PS_DRAW_BACKGROUND) + { + if (gl2ps->colormode == GL_RGBA || gl2ps->colorsize == 0) + { + glGetFloatv(GL_COLOR_CLEAR_VALUE, rgba) ; + } + else + { + glGetIntegerv(GL_INDEX_CLEAR_VALUE, &index) ; + rgba[0] = gl2ps->colormap[index][0] ; + rgba[1] = gl2ps->colormap[index][1] ; + rgba[2] = gl2ps->colormap[index][2] ; + rgba[3] = 1.0F ; } - gl2psPrintPGFColor(rgba); + gl2psPrintPGFColor(rgba) ; fprintf(gl2ps->stream, "\\pgfpathrectangle{\\pgfpoint{%dpt}{%dpt}}" - "{\\pgfpoint{%dpt}{%dpt}}\n" - "\\pgfusepath{fill}\n", x, y, w, h); + "{\\pgfpoint{%dpt}{%dpt}}\n" + "\\pgfusepath{fill}\n", + x, y, w, h) ; } fprintf(gl2ps->stream, "\\pgfpathrectangle{\\pgfpoint{%dpt}{%dpt}}" - "{\\pgfpoint{%dpt}{%dpt}}\n" - "\\pgfusepath{clip}\n", x, y, w, h); + "{\\pgfpoint{%dpt}{%dpt}}\n" + "\\pgfusepath{clip}\n", + x, y, w, h) ; } -static GLint gl2psPrintPGFEndViewport(void) { - GLint res; - res = gl2psPrintPrimitives(); - fprintf(gl2ps->stream, "\\end{pgfscope}\n"); - return res; +static GLint gl2psPrintPGFEndViewport(void) +{ + GLint res ; + res = gl2psPrintPrimitives() ; + fprintf(gl2ps->stream, "\\end{pgfscope}\n") ; + return res ; } -static void gl2psPrintPGFFinalPrimitive(void) { +static void gl2psPrintPGFFinalPrimitive(void) +{ } /* definition of the PGF backend */ static GL2PSbackend gl2psPGF = { gl2psPrintPGFHeader, gl2psPrintPGFFooter, - gl2psPrintPGFBeginViewport, gl2psPrintPGFEndViewport, - gl2psPrintPGFPrimitive, gl2psPrintPGFFinalPrimitive, "tex", - "PGF Latex Graphics" }; + gl2psPrintPGFBeginViewport, gl2psPrintPGFEndViewport, + gl2psPrintPGFPrimitive, gl2psPrintPGFFinalPrimitive, "tex", + "PGF Latex Graphics" } ; /********************************************************************* * @@ -5287,105 +5705,107 @@ static GL2PSbackend gl2psPGF = { gl2psPrintPGFHeader, gl2psPrintPGFFooter, #defines in gl2ps.h */ static GL2PSbackend *gl2psbackends[] = { &gl2psPS, /* 0 */ -&gl2psEPS, /* 1 */ -&gl2psTEX, /* 2 */ -&gl2psPDF, /* 3 */ -&gl2psSVG, /* 4 */ -&gl2psPGF /* 5 */ -}; - -static void gl2psComputeTightBoundingBox(void *data) { - GL2PSprimitive *prim; - int i; + &gl2psEPS, /* 1 */ + &gl2psTEX, /* 2 */ + &gl2psPDF, /* 3 */ + &gl2psSVG, /* 4 */ + &gl2psPGF /* 5 */ +} ; + +static void gl2psComputeTightBoundingBox(void *data) +{ + GL2PSprimitive *prim ; + int i ; - prim = *(GL2PSprimitive**) data; + prim = *(GL2PSprimitive**)data ; - for (i = 0; i < prim->numverts; i++) { - if (prim->verts[i].xyz[0] < gl2ps->viewport[0]) - gl2ps->viewport[0] = (GLint) prim->verts[i].xyz[0]; - if (prim->verts[i].xyz[0] > gl2ps->viewport[2]) - gl2ps->viewport[2] = (GLint)(prim->verts[i].xyz[0] + 0.5F); - if (prim->verts[i].xyz[1] < gl2ps->viewport[1]) - gl2ps->viewport[1] = (GLint) prim->verts[i].xyz[1]; - if (prim->verts[i].xyz[1] > gl2ps->viewport[3]) - gl2ps->viewport[3] = (GLint)(prim->verts[i].xyz[1] + 0.5F); + for (i = 0 ; i < prim->numverts; i++) + { + if (prim->verts[i].xyz[0] < gl2ps->viewport[0]) gl2ps->viewport[0] = (GLint)prim->verts[i].xyz[0] ; + if (prim->verts[i].xyz[0] > gl2ps->viewport[2]) gl2ps->viewport[2] = (GLint)( + prim->verts[i].xyz[0] + 0.5F) ; + if (prim->verts[i].xyz[1] < gl2ps->viewport[1]) gl2ps->viewport[1] = (GLint)prim->verts[i].xyz[1] ; + if (prim->verts[i].xyz[1] > gl2ps->viewport[3]) gl2ps->viewport[3] = (GLint)( + prim->verts[i].xyz[1] + 0.5F) ; } } -static GLint gl2psPrintPrimitives(void) { - GL2PSbsptree *root; - GL2PSxyz eye = { 0.0F, 0.0F, 100.0F * GL2PS_ZSCALE }; - GLint used; +static GLint gl2psPrintPrimitives(void) +{ + GL2PSbsptree *root ; + GL2PSxyz eye = { 0.0F, 0.0F, 100.0F * GL2PS_ZSCALE } ; + GLint used ; - used = glRenderMode(GL_RENDER); + used = glRenderMode(GL_RENDER) ; - if (used < 0) { - gl2psMsg(GL2PS_INFO, "OpenGL feedback buffer overflow"); - return GL2PS_OVERFLOW; + if (used < 0) + { + gl2psMsg(GL2PS_INFO, "OpenGL feedback buffer overflow") ; + return GL2PS_OVERFLOW ; } - if (used > 0) - gl2psParseFeedbackBuffer(used); + if (used > 0) gl2psParseFeedbackBuffer(used) ; - gl2psRescaleAndOffset(); + gl2psRescaleAndOffset() ; - if (gl2ps->header) { - if (gl2psListNbr(gl2ps->primitives) - && (gl2ps->options & GL2PS_TIGHT_BOUNDING_BOX)) { - gl2ps->viewport[0] = gl2ps->viewport[1] = 100000; - gl2ps->viewport[2] = gl2ps->viewport[3] = -100000; - gl2psListAction(gl2ps->primitives, gl2psComputeTightBoundingBox); + if (gl2ps->header) + { + if (gl2psListNbr(gl2ps->primitives) && (gl2ps->options & GL2PS_TIGHT_BOUNDING_BOX)) + { + gl2ps->viewport[0] = gl2ps->viewport[1] = 100000 ; + gl2ps->viewport[2] = gl2ps->viewport[3] = -100000 ; + gl2psListAction(gl2ps->primitives, gl2psComputeTightBoundingBox) ; } - (gl2psbackends[gl2ps->format]->printHeader)(); - gl2ps->header = GL_FALSE; + (gl2psbackends[gl2ps->format]->printHeader)() ; + gl2ps->header = GL_FALSE ; } - if (!gl2psListNbr(gl2ps->primitives)) { + if (!gl2psListNbr(gl2ps->primitives)) + { /* empty feedback buffer and/or nothing else to print */ - return GL2PS_NO_FEEDBACK; + return GL2PS_NO_FEEDBACK ; } - switch (gl2ps->sort) { - case GL2PS_NO_SORT: - gl2psListAction(gl2ps->primitives, - gl2psbackends[gl2ps->format]->printPrimitive); - gl2psListAction(gl2ps->primitives, gl2psFreePrimitive); + switch (gl2ps->sort) + { + case GL2PS_NO_SORT : + gl2psListAction(gl2ps->primitives, gl2psbackends[gl2ps->format]->printPrimitive) ; + gl2psListAction(gl2ps->primitives, gl2psFreePrimitive) ; /* reset the primitive list, waiting for the next viewport */ - gl2psListReset(gl2ps->primitives); - break; - case GL2PS_SIMPLE_SORT: - gl2psListSort(gl2ps->primitives, gl2psCompareDepth); - if (gl2ps->options & GL2PS_OCCLUSION_CULL) { - gl2psListActionInverse(gl2ps->primitives, gl2psAddInImageTree); - gl2psFreeBspImageTree(&gl2ps->imagetree); + gl2psListReset(gl2ps->primitives) ; + break ; + case GL2PS_SIMPLE_SORT : + gl2psListSort(gl2ps->primitives, gl2psCompareDepth) ; + if (gl2ps->options & GL2PS_OCCLUSION_CULL) + { + gl2psListActionInverse(gl2ps->primitives, gl2psAddInImageTree) ; + gl2psFreeBspImageTree(&gl2ps->imagetree) ; } - gl2psListAction(gl2ps->primitives, - gl2psbackends[gl2ps->format]->printPrimitive); - gl2psListAction(gl2ps->primitives, gl2psFreePrimitive); + gl2psListAction(gl2ps->primitives, gl2psbackends[gl2ps->format]->printPrimitive) ; + gl2psListAction(gl2ps->primitives, gl2psFreePrimitive) ; /* reset the primitive list, waiting for the next viewport */ - gl2psListReset(gl2ps->primitives); - break; - case GL2PS_BSP_SORT: - root = (GL2PSbsptree*) gl2psMalloc(sizeof(GL2PSbsptree)); - gl2psBuildBspTree(root, gl2ps->primitives); - if (GL_TRUE == gl2ps->boundary) - gl2psBuildPolygonBoundary(root); - if (gl2ps->options & GL2PS_OCCLUSION_CULL) { - gl2psTraverseBspTree(root, eye, -GL2PS_EPSILON, gl2psLess, - gl2psAddInImageTree, 1); - gl2psFreeBspImageTree(&gl2ps->imagetree); + gl2psListReset(gl2ps->primitives) ; + break ; + case GL2PS_BSP_SORT : + root = (GL2PSbsptree*)gl2psMalloc(sizeof(GL2PSbsptree)) ; + gl2psBuildBspTree(root, gl2ps->primitives) ; + if (GL_TRUE == gl2ps->boundary) gl2psBuildPolygonBoundary(root) ; + if (gl2ps->options & GL2PS_OCCLUSION_CULL) + { + gl2psTraverseBspTree(root, eye, -GL2PS_EPSILON, gl2psLess, gl2psAddInImageTree, 1) ; + gl2psFreeBspImageTree(&gl2ps->imagetree) ; } gl2psTraverseBspTree(root, eye, GL2PS_EPSILON, gl2psGreater, - gl2psbackends[gl2ps->format]->printPrimitive, 0); - gl2psFreeBspTree(&root); + gl2psbackends[gl2ps->format]->printPrimitive, 0) ; + gl2psFreeBspTree(&root) ; /* reallocate the primitive list (it's been deleted by gl2psBuildBspTree) in case there is another viewport */ - gl2ps->primitives = gl2psListCreate(500, 500, sizeof(GL2PSprimitive*)); - break; + gl2ps->primitives = gl2psListCreate(500, 500, sizeof(GL2PSprimitive*)) ; + break ; } - gl2psbackends[gl2ps->format]->printFinalPrimitive(); + gl2psbackends[gl2ps->format]->printFinalPrimitive() ; - return GL2PS_SUCCESS; + return GL2PS_SUCCESS ; } /********************************************************************* @@ -5394,474 +5814,494 @@ static GLint gl2psPrintPrimitives(void) { * *********************************************************************/ -GL2PSDLL_API GLint gl2psBeginPage(const char *title, const char *producer, - GLint viewport[4], GLint format, GLint sort, GLint options, - GLint colormode, GLint colorsize, GL2PSrgba *colormap, GLint nr, - GLint ng, GLint nb, GLint buffersize, FILE *stream, - const char *filename) { - GLint index; - int i; - - if (gl2ps) { - gl2psMsg(GL2PS_ERROR, "gl2psBeginPage called in wrong program state"); - return GL2PS_ERROR; - } - - gl2ps = (GL2PScontext*) gl2psMalloc(sizeof(GL2PScontext)); - - if (format >= 0 - && format - < (GLint)(sizeof(gl2psbackends) / sizeof(gl2psbackends[0]))) { - gl2ps->format = format; - } else { - gl2psMsg(GL2PS_ERROR, "Unknown output format: %d", format); - gl2psFree(gl2ps); - gl2ps = NULL; - return GL2PS_ERROR; - } - - switch (sort) { - case GL2PS_NO_SORT: - case GL2PS_SIMPLE_SORT: - case GL2PS_BSP_SORT: - gl2ps->sort = sort; - break; - default: - gl2psMsg(GL2PS_ERROR, "Unknown sorting algorithm: %d", sort); - gl2psFree(gl2ps); - gl2ps = NULL; - return GL2PS_ERROR; - } - - if (stream) { - gl2ps->stream = stream; - } else { - gl2psMsg(GL2PS_ERROR, "Bad file pointer"); - gl2psFree(gl2ps); - gl2ps = NULL; - return GL2PS_ERROR; - } - - gl2ps->header = GL_TRUE; - gl2ps->maxbestroot = 10; - gl2ps->options = options; - gl2ps->compress = NULL; - gl2ps->imagemap_head = NULL; - gl2ps->imagemap_tail = NULL; - - if (gl2ps->options & GL2PS_USE_CURRENT_VIEWPORT) { - glGetIntegerv(GL_VIEWPORT, gl2ps->viewport); - } else { - for (i = 0; i < 4; i++) { - gl2ps->viewport[i] = viewport[i]; +GL2PSDLL_API GLint gl2psBeginPage(const char *title, const char *producer, GLint viewport[4], + GLint format, GLint sort, GLint options, GLint colormode, + GLint colorsize, GL2PSrgba *colormap, GLint nr, GLint ng, + GLint nb, GLint buffersize, FILE *stream, const char *filename) +{ + GLint index ; + int i ; + + if (gl2ps) + { + gl2psMsg(GL2PS_ERROR, "gl2psBeginPage called in wrong program state") ; + return GL2PS_ERROR ; + } + + gl2ps = (GL2PScontext*)gl2psMalloc(sizeof(GL2PScontext)) ; + + if (format >= 0 && format < (GLint)(sizeof(gl2psbackends) / sizeof(gl2psbackends[0]))) + { + gl2ps->format = format ; + } + else + { + gl2psMsg(GL2PS_ERROR, "Unknown output format: %d", format) ; + gl2psFree(gl2ps) ; + gl2ps = NULL ; + return GL2PS_ERROR ; + } + + switch (sort) + { + case GL2PS_NO_SORT : + case GL2PS_SIMPLE_SORT : + case GL2PS_BSP_SORT : + gl2ps->sort = sort ; + break ; + default : + gl2psMsg(GL2PS_ERROR, "Unknown sorting algorithm: %d", sort) ; + gl2psFree(gl2ps) ; + gl2ps = NULL ; + return GL2PS_ERROR ; + } + + if (stream) + { + gl2ps->stream = stream ; + } + else + { + gl2psMsg(GL2PS_ERROR, "Bad file pointer") ; + gl2psFree(gl2ps) ; + gl2ps = NULL ; + return GL2PS_ERROR ; + } + + gl2ps->header = GL_TRUE ; + gl2ps->maxbestroot = 10 ; + gl2ps->options = options ; + gl2ps->compress = NULL ; + gl2ps->imagemap_head = NULL ; + gl2ps->imagemap_tail = NULL ; + + if (gl2ps->options & GL2PS_USE_CURRENT_VIEWPORT) + { + glGetIntegerv(GL_VIEWPORT, gl2ps->viewport) ; + } + else + { + for (i = 0 ; i < 4; i++) + { + gl2ps->viewport[i] = viewport[i] ; } } - if (!gl2ps->viewport[2] || !gl2ps->viewport[3]) { - gl2psMsg(GL2PS_ERROR, - "Incorrect viewport (x=%d, y=%d, width=%d, height=%d)", - gl2ps->viewport[0], gl2ps->viewport[1], gl2ps->viewport[2], - gl2ps->viewport[3]); - gl2psFree(gl2ps); - gl2ps = NULL; - return GL2PS_ERROR; - } - - gl2ps->threshold[0] = nr ? 1.0F / (GLfloat) nr : 0.064F; - gl2ps->threshold[1] = ng ? 1.0F / (GLfloat) ng : 0.034F; - gl2ps->threshold[2] = nb ? 1.0F / (GLfloat) nb : 0.100F; - gl2ps->colormode = colormode; - gl2ps->buffersize = buffersize > 0 ? buffersize : 2048 * 2048; - for (i = 0; i < 3; i++) { - gl2ps->lastvertex.xyz[i] = -1.0F; - } - for (i = 0; i < 4; i++) { - gl2ps->lastvertex.rgba[i] = -1.0F; - gl2ps->lastrgba[i] = -1.0F; - } - gl2ps->lastlinewidth = -1.0F; - gl2ps->lastpattern = 0; - gl2ps->lastfactor = 0; - gl2ps->imagetree = NULL; - gl2ps->primitivetoadd = NULL; - gl2ps->zerosurfacearea = GL_FALSE; - gl2ps->pdfprimlist = NULL; - gl2ps->pdfgrouplist = NULL; - gl2ps->xreflist = NULL; + if (!gl2ps->viewport[2] || !gl2ps->viewport[3]) + { + gl2psMsg(GL2PS_ERROR, "Incorrect viewport (x=%d, y=%d, width=%d, height=%d)", + gl2ps->viewport[0], gl2ps->viewport[1], gl2ps->viewport[2], gl2ps->viewport[3]) ; + gl2psFree(gl2ps) ; + gl2ps = NULL ; + return GL2PS_ERROR ; + } + + gl2ps->threshold[0] = nr ? 1.0F / (GLfloat)nr : 0.064F ; + gl2ps->threshold[1] = ng ? 1.0F / (GLfloat)ng : 0.034F ; + gl2ps->threshold[2] = nb ? 1.0F / (GLfloat)nb : 0.100F ; + gl2ps->colormode = colormode ; + gl2ps->buffersize = buffersize > 0 ? buffersize : 2048 * 2048 ; + for (i = 0 ; i < 3; i++) + { + gl2ps->lastvertex.xyz[i] = -1.0F ; + } + for (i = 0 ; i < 4; i++) + { + gl2ps->lastvertex.rgba[i] = -1.0F ; + gl2ps->lastrgba[i] = -1.0F ; + } + gl2ps->lastlinewidth = -1.0F ; + gl2ps->lastpattern = 0 ; + gl2ps->lastfactor = 0 ; + gl2ps->imagetree = NULL ; + gl2ps->primitivetoadd = NULL ; + gl2ps->zerosurfacearea = GL_FALSE ; + gl2ps->pdfprimlist = NULL ; + gl2ps->pdfgrouplist = NULL ; + gl2ps->xreflist = NULL ; /* get default blending mode from current OpenGL state (enabled by default for SVG) */ - gl2ps->blending = - (gl2ps->format == GL2PS_SVG) ? GL_TRUE : glIsEnabled(GL_BLEND); - glGetIntegerv(GL_BLEND_SRC, &gl2ps->blendfunc[0]); - glGetIntegerv(GL_BLEND_DST, &gl2ps->blendfunc[1]); - - if (gl2ps->colormode == GL_RGBA) { - gl2ps->colorsize = 0; - gl2ps->colormap = NULL; - glGetFloatv(GL_COLOR_CLEAR_VALUE, gl2ps->bgcolor); - } else if (gl2ps->colormode == GL_COLOR_INDEX) { - if (!colorsize || !colormap) { - gl2psMsg(GL2PS_ERROR, - "Missing colormap for GL_COLOR_INDEX rendering"); - gl2psFree(gl2ps); - gl2ps = NULL; - return GL2PS_ERROR; + gl2ps->blending = (gl2ps->format == GL2PS_SVG) ? GL_TRUE : glIsEnabled(GL_BLEND) ; + glGetIntegerv(GL_BLEND_SRC, &gl2ps->blendfunc[0]) ; + glGetIntegerv(GL_BLEND_DST, &gl2ps->blendfunc[1]) ; + + if (gl2ps->colormode == GL_RGBA) + { + gl2ps->colorsize = 0 ; + gl2ps->colormap = NULL ; + glGetFloatv(GL_COLOR_CLEAR_VALUE, gl2ps->bgcolor) ; + } + else if (gl2ps->colormode == GL_COLOR_INDEX) + { + if (!colorsize || !colormap) + { + gl2psMsg(GL2PS_ERROR, "Missing colormap for GL_COLOR_INDEX rendering") ; + gl2psFree(gl2ps) ; + gl2ps = NULL ; + return GL2PS_ERROR ; } - gl2ps->colorsize = colorsize; - gl2ps->colormap = (GL2PSrgba*) gl2psMalloc( - gl2ps->colorsize * sizeof(GL2PSrgba)); - memcpy(gl2ps->colormap, colormap, gl2ps->colorsize * sizeof(GL2PSrgba)); - glGetIntegerv(GL_INDEX_CLEAR_VALUE, &index); - gl2ps->bgcolor[0] = gl2ps->colormap[index][0]; - gl2ps->bgcolor[1] = gl2ps->colormap[index][1]; - gl2ps->bgcolor[2] = gl2ps->colormap[index][2]; - gl2ps->bgcolor[3] = 1.0F; - } else { - gl2psMsg(GL2PS_ERROR, "Unknown color mode in gl2psBeginPage"); - gl2psFree(gl2ps); - gl2ps = NULL; - return GL2PS_ERROR; - } - - if (!title) { - gl2ps->title = (char*) gl2psMalloc(sizeof(char)); - gl2ps->title[0] = '\0'; - } else { - gl2ps->title = (char*) gl2psMalloc((strlen(title) + 1) * sizeof(char)); - strcpy(gl2ps->title, title); - } - - if (!producer) { - gl2ps->producer = (char*) gl2psMalloc(sizeof(char)); - gl2ps->producer[0] = '\0'; - } else { - gl2ps->producer = (char*) gl2psMalloc( - (strlen(producer) + 1) * sizeof(char)); - strcpy(gl2ps->producer, producer); - } - - if (!filename) { - gl2ps->filename = (char*) gl2psMalloc(sizeof(char)); - gl2ps->filename[0] = '\0'; - } else { - gl2ps->filename = (char*) gl2psMalloc( - (strlen(filename) + 1) * sizeof(char)); - strcpy(gl2ps->filename, filename); - } - - gl2ps->primitives = gl2psListCreate(500, 500, sizeof(GL2PSprimitive*)); - gl2ps->auxprimitives = gl2psListCreate(100, 100, sizeof(GL2PSprimitive*)); - gl2ps->feedback = (GLfloat*) gl2psMalloc( - gl2ps->buffersize * sizeof(GLfloat)); - glFeedbackBuffer(gl2ps->buffersize, GL_3D_COLOR, gl2ps->feedback); - glRenderMode(GL_FEEDBACK); - - return GL2PS_SUCCESS; -} - -GL2PSDLL_API GLint gl2psEndPage(void) { - GLint res; + gl2ps->colorsize = colorsize ; + gl2ps->colormap = (GL2PSrgba*)gl2psMalloc(gl2ps->colorsize * sizeof(GL2PSrgba)) ; + memcpy(gl2ps->colormap, colormap, gl2ps->colorsize * sizeof(GL2PSrgba)) ; + glGetIntegerv(GL_INDEX_CLEAR_VALUE, &index) ; + gl2ps->bgcolor[0] = gl2ps->colormap[index][0] ; + gl2ps->bgcolor[1] = gl2ps->colormap[index][1] ; + gl2ps->bgcolor[2] = gl2ps->colormap[index][2] ; + gl2ps->bgcolor[3] = 1.0F ; + } + else + { + gl2psMsg(GL2PS_ERROR, "Unknown color mode in gl2psBeginPage") ; + gl2psFree(gl2ps) ; + gl2ps = NULL ; + return GL2PS_ERROR ; + } - if (!gl2ps) - return GL2PS_UNINITIALIZED; + if (!title) + { + gl2ps->title = (char*)gl2psMalloc(sizeof(char)) ; + gl2ps->title[0] = '\0' ; + } + else + { + gl2ps->title = (char*)gl2psMalloc((strlen(title) + 1) * sizeof(char)) ; + strcpy(gl2ps->title, title) ; + } + + if (!producer) + { + gl2ps->producer = (char*)gl2psMalloc(sizeof(char)) ; + gl2ps->producer[0] = '\0' ; + } + else + { + gl2ps->producer = (char*)gl2psMalloc((strlen(producer) + 1) * sizeof(char)) ; + strcpy(gl2ps->producer, producer) ; + } + + if (!filename) + { + gl2ps->filename = (char*)gl2psMalloc(sizeof(char)) ; + gl2ps->filename[0] = '\0' ; + } + else + { + gl2ps->filename = (char*)gl2psMalloc((strlen(filename) + 1) * sizeof(char)) ; + strcpy(gl2ps->filename, filename) ; + } + + gl2ps->primitives = gl2psListCreate(500, 500, sizeof(GL2PSprimitive*)) ; + gl2ps->auxprimitives = gl2psListCreate(100, 100, sizeof(GL2PSprimitive*)) ; + gl2ps->feedback = (GLfloat*)gl2psMalloc(gl2ps->buffersize * sizeof(GLfloat)) ; + glFeedbackBuffer(gl2ps->buffersize, GL_3D_COLOR, gl2ps->feedback) ; + glRenderMode(GL_FEEDBACK) ; + + return GL2PS_SUCCESS ; +} + +GL2PSDLL_API GLint gl2psEndPage(void) +{ + GLint res ; - res = gl2psPrintPrimitives(); + if (!gl2ps) return GL2PS_UNINITIALIZED ; - if (res != GL2PS_OVERFLOW) - (gl2psbackends[gl2ps->format]->printFooter)(); + res = gl2psPrintPrimitives() ; - fflush(gl2ps->stream); + if (res != GL2PS_OVERFLOW) (gl2psbackends[gl2ps->format]->printFooter)() ; - gl2psListDelete(gl2ps->primitives); - gl2psListDelete(gl2ps->auxprimitives); - gl2psFreeImagemap(gl2ps->imagemap_head); - gl2psFree(gl2ps->colormap); - gl2psFree(gl2ps->title); - gl2psFree(gl2ps->producer); - gl2psFree(gl2ps->filename); - gl2psFree(gl2ps->feedback); - gl2psFree(gl2ps); - gl2ps = NULL; + fflush(gl2ps->stream) ; - return res; + gl2psListDelete(gl2ps->primitives) ; + gl2psListDelete(gl2ps->auxprimitives) ; + gl2psFreeImagemap(gl2ps->imagemap_head) ; + gl2psFree(gl2ps->colormap) ; + gl2psFree(gl2ps->title) ; + gl2psFree(gl2ps->producer) ; + gl2psFree(gl2ps->filename) ; + gl2psFree(gl2ps->feedback) ; + gl2psFree(gl2ps) ; + gl2ps = NULL ; + + return res ; } -GL2PSDLL_API GLint gl2psBeginViewport(GLint viewport[4]) { - if (!gl2ps) - return GL2PS_UNINITIALIZED; +GL2PSDLL_API GLint gl2psBeginViewport(GLint viewport[4]) +{ + if (!gl2ps) return GL2PS_UNINITIALIZED ; - (gl2psbackends[gl2ps->format]->beginViewport)(viewport); + (gl2psbackends[gl2ps->format]->beginViewport)(viewport) ; - return GL2PS_SUCCESS; + return GL2PS_SUCCESS ; } -GL2PSDLL_API GLint gl2psEndViewport(void) { - GLint res; +GL2PSDLL_API GLint gl2psEndViewport(void) +{ + GLint res ; - if (!gl2ps) - return GL2PS_UNINITIALIZED; + if (!gl2ps) return GL2PS_UNINITIALIZED ; - res = (gl2psbackends[gl2ps->format]->endViewport)(); + res = (gl2psbackends[gl2ps->format]->endViewport)() ; /* reset last used colors, line widths */ - gl2ps->lastlinewidth = -1.0F; + gl2ps->lastlinewidth = -1.0F ; - return res; + return res ; } -GL2PSDLL_API GLint gl2psTextOpt(const char *str, const char *fontname, - GLshort fontsize, GLint alignment, GLfloat angle) { - return gl2psAddText(GL2PS_TEXT, str, fontname, fontsize, alignment, angle); +GL2PSDLL_API GLint gl2psTextOpt(const char *str, const char *fontname, GLshort fontsize, + GLint alignment, GLfloat angle) +{ + return gl2psAddText(GL2PS_TEXT, str, fontname, fontsize, alignment, angle) ; } -GL2PSDLL_API GLint gl2psText(const char *str, const char *fontname, - GLshort fontsize) { - return gl2psAddText(GL2PS_TEXT, str, fontname, fontsize, GL2PS_TEXT_BL, - 0.0F); +GL2PSDLL_API GLint gl2psText(const char *str, const char *fontname, GLshort fontsize) +{ + return gl2psAddText(GL2PS_TEXT, str, fontname, fontsize, GL2PS_TEXT_BL, 0.0F) ; } -GL2PSDLL_API GLint gl2psSpecial(GLint format, const char *str) { - return gl2psAddText(GL2PS_SPECIAL, str, "", 0, format, 0.0F); +GL2PSDLL_API GLint gl2psSpecial(GLint format, const char *str) +{ + return gl2psAddText(GL2PS_SPECIAL, str, "", 0, format, 0.0F) ; } -GL2PSDLL_API GLint gl2psDrawPixels(GLsizei width, GLsizei height, GLint xorig, - GLint yorig, GLenum format, GLenum type, const void *pixels) { - int size, i; - GLfloat pos[4], *piv; - GL2PSprimitive *prim; - GLboolean valid; +GL2PSDLL_API GLint gl2psDrawPixels(GLsizei width, GLsizei height, GLint xorig, GLint yorig, + GLenum format, GLenum type, const void *pixels) +{ + int size, i ; + GLfloat pos[4], *piv ; + GL2PSprimitive *prim ; + GLboolean valid ; - if (!gl2ps || !pixels) - return GL2PS_UNINITIALIZED; + if (!gl2ps || !pixels) return GL2PS_UNINITIALIZED ; - if ((width <= 0) || (height <= 0)) - return GL2PS_ERROR; + if ((width <= 0) || (height <= 0)) return GL2PS_ERROR ; - if (gl2ps->options & GL2PS_NO_PIXMAP) - return GL2PS_SUCCESS; + if (gl2ps->options & GL2PS_NO_PIXMAP) return GL2PS_SUCCESS ; - if ((format != GL_RGB && format != GL_RGBA) || type != GL_FLOAT) { + if ((format != GL_RGB && format != GL_RGBA) || type != GL_FLOAT) + { gl2psMsg(GL2PS_ERROR, "gl2psDrawPixels only implemented for " - "GL_RGB/GL_RGBA, GL_FLOAT pixels"); - return GL2PS_ERROR; - } - - glGetBooleanv(GL_CURRENT_RASTER_POSITION_VALID, &valid); - if (GL_FALSE == valid) - return GL2PS_SUCCESS; /* the primitive is culled */ - - glGetFloatv(GL_CURRENT_RASTER_POSITION, pos); - - prim = (GL2PSprimitive*) gl2psMalloc(sizeof(GL2PSprimitive)); - prim->type = GL2PS_PIXMAP; - prim->boundary = 0; - prim->numverts = 1; - prim->verts = (GL2PSvertex*) gl2psMalloc(sizeof(GL2PSvertex)); - prim->verts[0].xyz[0] = pos[0] + xorig; - prim->verts[0].xyz[1] = pos[1] + yorig; - prim->verts[0].xyz[2] = pos[2]; - prim->culled = 0; - prim->offset = 0; - prim->pattern = 0; - prim->factor = 0; - prim->width = 1; - glGetFloatv(GL_CURRENT_RASTER_COLOR, prim->verts[0].rgba); - prim->data.image = (GL2PSimage*) gl2psMalloc(sizeof(GL2PSimage)); - prim->data.image->width = width; - prim->data.image->height = height; - prim->data.image->format = format; - prim->data.image->type = type; - - switch (format) { - case GL_RGBA: - if (gl2ps->options & GL2PS_NO_BLENDING || !gl2ps->blending) { + "GL_RGB/GL_RGBA, GL_FLOAT pixels") ; + return GL2PS_ERROR ; + } + + glGetBooleanv(GL_CURRENT_RASTER_POSITION_VALID, &valid) ; + if (GL_FALSE == valid) return GL2PS_SUCCESS ; /* the primitive is culled */ + + glGetFloatv(GL_CURRENT_RASTER_POSITION, pos) ; + + prim = (GL2PSprimitive*)gl2psMalloc(sizeof(GL2PSprimitive)) ; + prim->type = GL2PS_PIXMAP ; + prim->boundary = 0 ; + prim->numverts = 1 ; + prim->verts = (GL2PSvertex*)gl2psMalloc(sizeof(GL2PSvertex)) ; + prim->verts[0].xyz[0] = pos[0] + xorig ; + prim->verts[0].xyz[1] = pos[1] + yorig ; + prim->verts[0].xyz[2] = pos[2] ; + prim->culled = 0 ; + prim->offset = 0 ; + prim->pattern = 0 ; + prim->factor = 0 ; + prim->width = 1 ; + glGetFloatv(GL_CURRENT_RASTER_COLOR, prim->verts[0].rgba) ; + prim->data.image = (GL2PSimage*)gl2psMalloc(sizeof(GL2PSimage)) ; + prim->data.image->width = width ; + prim->data.image->height = height ; + prim->data.image->format = format ; + prim->data.image->type = type ; + + switch (format) + { + case GL_RGBA : + if (gl2ps->options & GL2PS_NO_BLENDING || !gl2ps->blending) + { /* special case: blending turned off */ - prim->data.image->format = GL_RGB; - size = height * width * 3; - prim->data.image->pixels = (GLfloat*) gl2psMalloc( - size * sizeof(GLfloat)); - piv = (GLfloat*) pixels; - for (i = 0; i < size; ++i, ++piv) { - prim->data.image->pixels[i] = *piv; - if (!((i + 1) % 3)) - ++piv; + prim->data.image->format = GL_RGB ; + size = height * width * 3 ; + prim->data.image->pixels = (GLfloat*)gl2psMalloc(size * sizeof(GLfloat)) ; + piv = (GLfloat*)pixels ; + for (i = 0 ; i < size; ++i, ++piv) + { + prim->data.image->pixels[i] = *piv ; + if (!((i + 1) % 3)) ++piv ; } - } else { - size = height * width * 4; - prim->data.image->pixels = (GLfloat*) gl2psMalloc( - size * sizeof(GLfloat)); - memcpy(prim->data.image->pixels, pixels, size * sizeof(GLfloat)); } - break; - case GL_RGB: - default: - size = height * width * 3; - prim->data.image->pixels = (GLfloat*) gl2psMalloc( - size * sizeof(GLfloat)); - memcpy(prim->data.image->pixels, pixels, size * sizeof(GLfloat)); - break; + else + { + size = height * width * 4 ; + prim->data.image->pixels = (GLfloat*)gl2psMalloc(size * sizeof(GLfloat)) ; + memcpy(prim->data.image->pixels, pixels, size * sizeof(GLfloat)) ; + } + break ; + case GL_RGB : + default : + size = height * width * 3 ; + prim->data.image->pixels = (GLfloat*)gl2psMalloc(size * sizeof(GLfloat)) ; + memcpy(prim->data.image->pixels, pixels, size * sizeof(GLfloat)) ; + break ; } - gl2psListAdd(gl2ps->auxprimitives, &prim); - glPassThrough(GL2PS_DRAW_PIXELS_TOKEN); + gl2psListAdd(gl2ps->auxprimitives, &prim) ; + glPassThrough(GL2PS_DRAW_PIXELS_TOKEN) ; - return GL2PS_SUCCESS; + return GL2PS_SUCCESS ; } -GL2PSDLL_API GLint gl2psDrawImageMap(GLsizei width, GLsizei height, - const GLfloat position[3], const unsigned char *imagemap) { - int size, i; - int sizeoffloat = sizeof(GLfloat); +GL2PSDLL_API GLint gl2psDrawImageMap(GLsizei width, GLsizei height, const GLfloat position[3], + const unsigned char *imagemap) +{ + int size, i ; + int sizeoffloat = sizeof(GLfloat) ; - if (!gl2ps || !imagemap) - return GL2PS_UNINITIALIZED; + if (!gl2ps || !imagemap) return GL2PS_UNINITIALIZED ; - if ((width <= 0) || (height <= 0)) - return GL2PS_ERROR; + if ((width <= 0) || (height <= 0)) return GL2PS_ERROR ; - size = height + height * ((width - 1) / 8); - glPassThrough(GL2PS_IMAGEMAP_TOKEN); - glBegin(GL_POINTS); - glVertex3f(position[0], position[1], position[2]); - glEnd(); - glPassThrough((GLfloat) width); - glPassThrough((GLfloat) height); - for (i = 0; i < size; i += sizeoffloat) { - float *value = (float*) imagemap; - glPassThrough(*value); - imagemap += sizeoffloat; + size = height + height * ((width - 1) / 8) ; + glPassThrough(GL2PS_IMAGEMAP_TOKEN) ; + glBegin(GL_POINTS) ; + glVertex3f(position[0], position[1], position[2]) ; + glEnd() ; + glPassThrough((GLfloat)width) ; + glPassThrough((GLfloat)height) ; + for (i = 0 ; i < size; i += sizeoffloat) + { + float *value = (float*)imagemap ; + glPassThrough(*value) ; + imagemap += sizeoffloat ; } - return GL2PS_SUCCESS; + return GL2PS_SUCCESS ; } -GL2PSDLL_API GLint gl2psEnable(GLint mode) { - GLint tmp; - - if (!gl2ps) - return GL2PS_UNINITIALIZED; - - switch (mode) { - case GL2PS_POLYGON_OFFSET_FILL: - glPassThrough(GL2PS_BEGIN_OFFSET_TOKEN); - glGetFloatv(GL_POLYGON_OFFSET_FACTOR, &gl2ps->offset[0]); - glGetFloatv(GL_POLYGON_OFFSET_UNITS, &gl2ps->offset[1]); - break; - case GL2PS_POLYGON_BOUNDARY: - glPassThrough(GL2PS_BEGIN_BOUNDARY_TOKEN); - break; - case GL2PS_LINE_STIPPLE: - glPassThrough(GL2PS_BEGIN_STIPPLE_TOKEN); - glGetIntegerv(GL_LINE_STIPPLE_PATTERN, &tmp); - glPassThrough((GLfloat) tmp); - glGetIntegerv(GL_LINE_STIPPLE_REPEAT, &tmp); - glPassThrough((GLfloat) tmp); - break; - case GL2PS_BLEND: - glPassThrough(GL2PS_BEGIN_BLEND_TOKEN); - break; - default: - gl2psMsg(GL2PS_WARNING, "Unknown mode in gl2psEnable: %d", mode); - return GL2PS_WARNING; - } - - return GL2PS_SUCCESS; -} - -GL2PSDLL_API GLint gl2psDisable(GLint mode) { - if (!gl2ps) - return GL2PS_UNINITIALIZED; - - switch (mode) { - case GL2PS_POLYGON_OFFSET_FILL: - glPassThrough(GL2PS_END_OFFSET_TOKEN); - break; - case GL2PS_POLYGON_BOUNDARY: - glPassThrough(GL2PS_END_BOUNDARY_TOKEN); - break; - case GL2PS_LINE_STIPPLE: - glPassThrough(GL2PS_END_STIPPLE_TOKEN); - break; - case GL2PS_BLEND: - glPassThrough(GL2PS_END_BLEND_TOKEN); - break; - default: - gl2psMsg(GL2PS_WARNING, "Unknown mode in gl2psDisable: %d", mode); - return GL2PS_WARNING; - } - - return GL2PS_SUCCESS; -} - -GL2PSDLL_API GLint gl2psPointSize(GLfloat value) { - if (!gl2ps) - return GL2PS_UNINITIALIZED; +GL2PSDLL_API GLint gl2psEnable(GLint mode) +{ + GLint tmp ; + + if (!gl2ps) return GL2PS_UNINITIALIZED ; + + switch (mode) + { + case GL2PS_POLYGON_OFFSET_FILL : + glPassThrough(GL2PS_BEGIN_OFFSET_TOKEN) ; + glGetFloatv(GL_POLYGON_OFFSET_FACTOR, &gl2ps->offset[0]) ; + glGetFloatv(GL_POLYGON_OFFSET_UNITS, &gl2ps->offset[1]) ; + break ; + case GL2PS_POLYGON_BOUNDARY : + glPassThrough(GL2PS_BEGIN_BOUNDARY_TOKEN) ; + break ; + case GL2PS_LINE_STIPPLE : + glPassThrough(GL2PS_BEGIN_STIPPLE_TOKEN) ; + glGetIntegerv(GL_LINE_STIPPLE_PATTERN, &tmp) ; + glPassThrough((GLfloat)tmp) ; + glGetIntegerv(GL_LINE_STIPPLE_REPEAT, &tmp) ; + glPassThrough((GLfloat)tmp) ; + break ; + case GL2PS_BLEND : + glPassThrough(GL2PS_BEGIN_BLEND_TOKEN) ; + break ; + default : + gl2psMsg(GL2PS_WARNING, "Unknown mode in gl2psEnable: %d", mode) ; + return GL2PS_WARNING ; + } + + return GL2PS_SUCCESS ; +} + +GL2PSDLL_API GLint gl2psDisable(GLint mode) +{ + if (!gl2ps) return GL2PS_UNINITIALIZED ; + + switch (mode) + { + case GL2PS_POLYGON_OFFSET_FILL : + glPassThrough(GL2PS_END_OFFSET_TOKEN) ; + break ; + case GL2PS_POLYGON_BOUNDARY : + glPassThrough(GL2PS_END_BOUNDARY_TOKEN) ; + break ; + case GL2PS_LINE_STIPPLE : + glPassThrough(GL2PS_END_STIPPLE_TOKEN) ; + break ; + case GL2PS_BLEND : + glPassThrough(GL2PS_END_BLEND_TOKEN) ; + break ; + default : + gl2psMsg(GL2PS_WARNING, "Unknown mode in gl2psDisable: %d", mode) ; + return GL2PS_WARNING ; + } + + return GL2PS_SUCCESS ; +} + +GL2PSDLL_API GLint gl2psPointSize(GLfloat value) +{ + if (!gl2ps) return GL2PS_UNINITIALIZED ; - glPassThrough(GL2PS_POINT_SIZE_TOKEN); - glPassThrough(value); + glPassThrough(GL2PS_POINT_SIZE_TOKEN) ; + glPassThrough(value) ; - return GL2PS_SUCCESS; + return GL2PS_SUCCESS ; } -GL2PSDLL_API GLint gl2psLineWidth(GLfloat value) { - if (!gl2ps) - return GL2PS_UNINITIALIZED; +GL2PSDLL_API GLint gl2psLineWidth(GLfloat value) +{ + if (!gl2ps) return GL2PS_UNINITIALIZED ; - glPassThrough(GL2PS_LINE_WIDTH_TOKEN); - glPassThrough(value); + glPassThrough(GL2PS_LINE_WIDTH_TOKEN) ; + glPassThrough(value) ; - return GL2PS_SUCCESS; + return GL2PS_SUCCESS ; } -GL2PSDLL_API GLint gl2psBlendFunc(GLenum sfactor, GLenum dfactor) { - if (!gl2ps) - return GL2PS_UNINITIALIZED; +GL2PSDLL_API GLint gl2psBlendFunc(GLenum sfactor, GLenum dfactor) +{ + if (!gl2ps) return GL2PS_UNINITIALIZED ; - if (GL_FALSE == gl2psSupportedBlendMode(sfactor, dfactor)) - return GL2PS_WARNING; + if (GL_FALSE == gl2psSupportedBlendMode(sfactor, dfactor)) return GL2PS_WARNING ; - glPassThrough(GL2PS_SRC_BLEND_TOKEN); - glPassThrough((GLfloat) sfactor); - glPassThrough(GL2PS_DST_BLEND_TOKEN); - glPassThrough((GLfloat) dfactor); + glPassThrough(GL2PS_SRC_BLEND_TOKEN) ; + glPassThrough((GLfloat)sfactor) ; + glPassThrough(GL2PS_DST_BLEND_TOKEN) ; + glPassThrough((GLfloat)dfactor) ; - return GL2PS_SUCCESS; + return GL2PS_SUCCESS ; } -GL2PSDLL_API GLint gl2psSetOptions(GLint options) { - if (!gl2ps) - return GL2PS_UNINITIALIZED; +GL2PSDLL_API GLint gl2psSetOptions(GLint options) +{ + if (!gl2ps) return GL2PS_UNINITIALIZED ; - gl2ps->options = options; + gl2ps->options = options ; - return GL2PS_SUCCESS; + return GL2PS_SUCCESS ; } -GL2PSDLL_API GLint gl2psGetOptions(GLint *options) { - if (!gl2ps) { - *options = 0; - return GL2PS_UNINITIALIZED; +GL2PSDLL_API GLint gl2psGetOptions(GLint *options) +{ + if (!gl2ps) + { + *options = 0 ; + return GL2PS_UNINITIALIZED ; } - *options = gl2ps->options; + *options = gl2ps->options ; - return GL2PS_SUCCESS; + return GL2PS_SUCCESS ; } -GL2PSDLL_API const char *gl2psGetFileExtension(GLint format) { - if (format >= 0 - && format - < (GLint)(sizeof(gl2psbackends) / sizeof(gl2psbackends[0]))) - return gl2psbackends[format]->file_extension; +GL2PSDLL_API const char *gl2psGetFileExtension(GLint format) +{ + if (format >= 0 && format < (GLint)(sizeof(gl2psbackends) / sizeof(gl2psbackends[0]))) + return gl2psbackends[format]->file_extension ; else - return "Unknown format"; + return "Unknown format" ; } -GL2PSDLL_API const char *gl2psGetFormatDescription(GLint format) { - if (format >= 0 - && format - < (GLint)(sizeof(gl2psbackends) / sizeof(gl2psbackends[0]))) - return gl2psbackends[format]->description; +GL2PSDLL_API const char *gl2psGetFormatDescription(GLint format) +{ + if (format >= 0 && format < (GLint)(sizeof(gl2psbackends) / sizeof(gl2psbackends[0]))) + return gl2psbackends[format]->description ; else - return "Unknown format"; + return "Unknown format" ; } diff --git a/src/simulator.cpp b/src/simulator.cpp index 99e6d9087c2b421e275332bde19251fb70def6f7..ec853b11ba1bf6b633b0d364fef605e39eb2641b 100644 --- a/src/simulator.cpp +++ b/src/simulator.cpp @@ -1,54 +1,59 @@ #include "simulator.h" #include -Simulator::Simulator() : - timeStep_(0.2f), globalTime_(0.0f), nbSteps_(0), - nbUpdates(0), nbSorts(0), nbRefineCandidate(0), nbCoarsenCandidate(0), - nearNeighbors(0), totalNeighbors(0) +Simulator::Simulator(int minSize) : + timeStep_(0.2f), + globalTime_(0.0f), + nbSteps_(0), + nbUpdates(0), + nbSorts(0), + nbRefineCandidate(0), + nbCoarsenCandidate(0), + nearNeighbors(0), + totalNeighbors(0) { srand(10) ; nbStepsPerUnit_ = 1 / timeStep_ ; - init(1, 2.0f) ; + init(1, minSize, 2.0f) ; } Simulator::~Simulator() { - for (unsigned int i = 0 ; i < agents_.size() ; ++i) + for (unsigned int i = 0; i < agents_.size(); ++i) delete agents_[i] ; } -void Simulator::init(unsigned int config, float dimension, bool enablePathFinding) +void Simulator::init(unsigned int config, int minSize, float dimension, bool enablePathFinding) { std::cout << "Setup scenario" << std::endl ; -#ifdef SPATIAL_HASHING - envMap_.init(config, 2500.0f, 2000.0f, Agent::neighborDist_ , 200.0f) ; -#else - envMap_.init(config, 2500.0f, 2000.0f, Agent::neighborDist_/4.0f , 200.0f) ; -#endif + envMap_.init(config, 2500.0f, 2000.0f, minSize, 200.0f) ; - switch (config) { - case 0 : - setupCircleScenario(1000) ; - break ; - case 1 : - setupCorridorScenario(5000) ; - break ; - case 2 : - setupScenario(1000) ; - break ; - case 3 : - setupCityScenario( -1.0f * (12 * (70.0f / 2.0f) - 10), - -1.0f * (12 * (70.0f / 2.0f) - 10), 20, 20) ; - break ; - case 4 : - importAgents("myAgents.pos") ; - break ; + switch (config) + { + case 0 : + setupCircleScenario(1000) ; + break ; + case 1 : + setupCorridorScenario(5000) ; + break ; + case 2 : + setupScenario(1000) ; + break ; + case 3 : + setupCityScenario(-1.0f * (12 * (70.0f / 2.0f) - 10), -1.0f * (12 * (70.0f / 2.0f) - 10), + 20, 20) ; + break ; + case 4 : + importAgents("myAgents.pos") ; + break ; } #ifndef SPATIAL_HASHING - if (enablePathFinding) { - if (dimension == 2.0f) addPathsToAgents() ; + if (enablePathFinding) + { + if (dimension == 2.0f) + addPathsToAgents() ; else if (dimension == 2.5f) addPathsToAgents_height() ; } #endif @@ -59,7 +64,8 @@ void Simulator::init(unsigned int config, float dimension, bool enablePathFindin // addPathToCorner(); - for (unsigned int i = 0 ; i < nbAgents ; ++i) { + for (unsigned int i = 0; i < nbAgents; ++i) + { agents_[i]->updateObstacleNeighbors() ; agents_[i]->updateAgentNeighbors() ; } @@ -76,13 +82,14 @@ void Simulator::init(unsigned int config, float dimension, bool enablePathFindin void Simulator::setupCircleScenario(unsigned int nbMaxAgent) { - std::cout << " - Setup Circle Scenario : " << nbMaxAgent << " agents" << std::endl; + std::cout << " - Setup Circle Scenario : " << nbMaxAgent << " agents" << std::endl ; float pi = 3.14159265358979323846f ; float r = 800.0f ; - for (unsigned int i = 0 ; i < nbMaxAgent ; ++i) { + for (unsigned int i = 0; i < nbMaxAgent; ++i) + { VEC3 posagent(std::cos(i * 2.0f * pi / float(nbMaxAgent)) * r, - std::sin(i * 2.0f * pi / float(nbMaxAgent)) * r, 0) ; + std::sin(i * 2.0f * pi / float(nbMaxAgent)) * r, 0) ; #ifdef SPATIAL_HASHING Geom::Vec2ui c = envMap_.agentPositionCell(posagent) ; @@ -105,35 +112,37 @@ void Simulator::setupCircleScenario(unsigned int nbMaxAgent) agents_.push_back(new Agent(this, posagent, dCell)) ; #endif - agents_.back()->goals_.push_back( -1.0f * posagent) ; + agents_.back()->goals_.push_back(-1.0f * posagent) ; agents_.back()->curGoal_ = 0 ; } std::cout << "nb agents : " << agents_.size() << std::endl ; #ifndef SPATIAL_HASHING - for (unsigned int i = 0 ; i < agents_.size() ; ++i) + for (unsigned int i = 0; i < agents_.size(); ++i) envMap_.pushAgentInCells(agents_[i], agents_[i]->part_.d) ; #endif } void Simulator::setupCorridorScenario(unsigned int nbMaxAgent) { - std::cout << " - Setup Corridor Scenario : " << nbMaxAgent << " agents" << std::endl; + std::cout << " - Setup Corridor Scenario : " << nbMaxAgent << " agents" << std::endl ; float left = -600.0f ; float variationX = 120.0f ; float right = 600.0f ; float middle = 0.0f ; float variationY = 400.0f ; - for (unsigned int i = 0 ; i < nbMaxAgent ; ++i) { - VEC3 posagent(left + std::cos(rand()) * variationX, - middle + std::sin(rand()) * variationY, 0) ; - VEC3 posgoal(right + std::cos(rand()) * variationX, - middle + std::sin(rand()) * variationY, 0) ; + for (unsigned int i = 0; i < nbMaxAgent; ++i) + { + VEC3 posagent(left + std::cos(rand()) * variationX, middle + std::sin(rand()) * variationY, + 0) ; + VEC3 posgoal(right + std::cos(rand()) * variationX, middle + std::sin(rand()) * variationY, + 0) ; VEC3 tmp ; - if (i % 2 == 1) { + if (i % 2 == 1) + { tmp = posgoal ; posgoal = posagent ; posagent = tmp ; @@ -167,7 +176,7 @@ void Simulator::setupCorridorScenario(unsigned int nbMaxAgent) std::cout << "nb agents : " << agents_.size() << std::endl ; #ifndef SPATIAL_HASHING - for (unsigned int i = 0 ; i < agents_.size() ; ++i) + for (unsigned int i = 0; i < agents_.size(); ++i) envMap_.pushAgentInCells(agents_[i], agents_[i]->part_.d) ; #endif } @@ -198,11 +207,13 @@ void Simulator::setupCityScenario(float startX, float startY, int nbLines, int n Dart dCell = envMap_.getBelongingCell(posagent) ; agents_.push_back(new Agent(this, posagent, dCell)) ; #endif - agents_.back()->goals_.push_back( -1.0f * posagent) ; + agents_.back()->goals_.push_back(-1.0f * posagent) ; agents_.back()->curGoal_ = 0 ; - for (int i = 0 ; i < nbLines ; ++i) { - for (int j = 0 ; j < nbRank ; ++j) { + for (int i = 0; i < nbLines; ++i) + { + for (int j = 0; j < nbRank; ++j) + { VEC3 posagent(xCornerDown + i * 10.0f, yCornerDown + j * 10.0f, 0.0f) ; #ifdef SPATIAL_HASHING Geom::Vec2ui c = envMap_.agentPositionCell(posagent) ; @@ -224,7 +235,7 @@ void Simulator::setupCityScenario(float startX, float startY, int nbLines, int n Dart dCell = envMap_.getBelongingCell(posagent) ; agents_.push_back(new Agent(this, posagent, dCell)) ; #endif - agents_.back()->goals_.push_back( -1.0f * posagent) ; + agents_.back()->goals_.push_back(-1.0f * posagent) ; agents_.back()->curGoal_ = 0 ; } } @@ -232,7 +243,7 @@ void Simulator::setupCityScenario(float startX, float startY, int nbLines, int n std::cout << "nb agents : " << agents_.size() << std::endl ; #ifndef SPATIAL_HASHING - for (unsigned int i = 0 ; i < agents_.size() ; ++i) + for (unsigned int i = 0; i < agents_.size(); ++i) envMap_.pushAgentInCells(agents_[i], agents_[i]->part_.d) ; #endif } @@ -272,13 +283,16 @@ void Simulator::setupScenario(unsigned int nbMaxAgent) unsigned int bMax = nbx * nby > 0 ? nbMaxAgent / (nbx * nby) : nbMaxAgent ; - for (unsigned int i = 0 ; i < bMax && d != envMap_.map.end() ; ++i) { + for (unsigned int i = 0; i < bMax && d != envMap_.map.end(); ++i) + { bool found = false ; VEC3 pos ; Dart dCell ; - while ( !found && d != envMap_.map.end()) { - if ( !filled.isMarked(d) - && !envMap_.buildingMark.isMarked(d) /*&& envMap_.pedWayMark.isMarked(d)*/) { + while (!found && d != envMap_.map.end()) + { + if (!filled.isMarked(d) + && !envMap_.buildingMark.isMarked(d) /*&& envMap_.pedWayMark.isMarked(d)*/) + { filled.mark(d) ; pos = Algo::Geometry::faceCentroid(envMap_.map, d, envMap_.position) ; pos[2] = 0 ; @@ -288,12 +302,15 @@ void Simulator::setupScenario(unsigned int nbMaxAgent) envMap_.map.next(d) ; } - if (found) { + if (found) + { float ecart = 3.0f ; VEC3 posinit = VEC3(pos[0] - (float(nbx) / 2.0f * ecart), - pos[1] - (float(nby) / 2.0f * ecart), pos[2]) ; - for (unsigned int curx = 0 ; curx < nbx ; ++curx) { - for (unsigned int cury = 0 ; cury < nby ; ++cury) { + pos[1] - (float(nby) / 2.0f * ecart), pos[2]) ; + for (unsigned int curx = 0; curx < nbx; ++curx) + { + for (unsigned int cury = 0; cury < nby; ++cury) + { VEC3 posagent = posinit + VEC3(ecart * curx, ecart * cury, 0.0f) ; #ifdef SPATIAL_HASHING Geom::Vec2ui c = envMap_.agentPositionCell(posagent) ; @@ -316,7 +333,7 @@ void Simulator::setupScenario(unsigned int nbMaxAgent) #endif agents_.back()->goals_.push_back(posagent) ; // agents_.back()->goals_.push_back(VEC3(0,0,0)); - agents_.back()->goals_.push_back( -1.0f * posagent) ; + agents_.back()->goals_.push_back(-1.0f * posagent) ; agents_.back()->curGoal_ = 1 ; } } @@ -327,7 +344,7 @@ void Simulator::setupScenario(unsigned int nbMaxAgent) swapAgentsGoals() ; #ifndef SPATIAL_HASHING - for (unsigned int i = 0 ; i < agents_.size() ; ++i) + for (unsigned int i = 0; i < agents_.size(); ++i) envMap_.pushAgentInCells(agents_[i], agents_[i]->part_.d) ; #endif } @@ -348,7 +365,8 @@ void Simulator::doStep() // movingObstacles_[i]->update(); // } - for (unsigned int i = 0 ; i < agents_.size() ; ++i) { + for (unsigned int i = 0; i < agents_.size(); ++i) + { agents_[i]->updateObstacleNeighbors() ; #ifndef SPATIAL_HASHING @@ -419,14 +437,15 @@ void Simulator::doStep() #else envMap_.map.setCurrentLevel(envMap_.map.getMaxLevel()) ; - for (unsigned int i = 0 ; i < agents_.size() ; ++i) { + for (unsigned int i = 0; i < agents_.size(); ++i) + { Dart oldFace = agents_[i]->part_.d ; agents_[i]->update() ; // if(envMap_.map.getEmbedding(oldFace, FACE) != envMap_.map.getEmbedding(agents_[i]->part_.d, FACE)) if (agents_[i]->part_.crossCell != CGoGN::Algo::MovingObjects::NO_CROSS) // switch(agents_[i]->part_.crossCell) - { + { // case CGoGN::Algo::MovingObjects::CROSS_EDGE : // envMap_.agentChangeFaceThroughEdge(agents_[i]); // break; @@ -455,9 +474,9 @@ void Simulator::doStep() bool Simulator::reachedGoal() { /* Check if all agents have reached their goals. */ - for (unsigned int i = 0 ; i < agents_.size() ; ++i) + for (unsigned int i = 0; i < agents_.size(); ++i) if ((agents_[i]->getPosition() - agents_[i]->goals_[agents_[i]->curGoal_]).norm2() - > agents_[i]->radius_ * agents_[i]->radius_) return false ; + > agents_[i]->radius_ * agents_[i]->radius_) return false ; return true ; } @@ -465,7 +484,8 @@ bool Simulator::reachedGoal() #ifndef SPATIAL_HASHING void Simulator::addPathToCorner() { - for (unsigned int i = 0 ; i < agents_.size() ; ++i) { + for (unsigned int i = 0; i < agents_.size(); ++i) + { agents_[i]->goals_.clear() ; agents_.back()->curGoal_ = 1 ; @@ -473,9 +493,12 @@ void Simulator::addPathToCorner() Dart dStop = agents_[i]->finalDart ; std::vector path = CGoGN::PathFinder::pathFindAStar(envMap_.map, - envMap_.position, dStart, dStop, envMap_.buildingMark) ; + envMap_.position, dStart, + dStop, + envMap_.buildingMark) ; - for (std::vector::iterator it = path.begin() ; it != path.end() ; ++it) { + for (std::vector::iterator it = path.begin(); it != path.end(); ++it) + { VEC3 dest = Algo::Geometry::faceCentroid(envMap_.map, *it, envMap_.position) ; // VEC3 dest = envMap_.position[*it] + envMap_.position[envMap_.map.phi1(*it)]; @@ -491,14 +514,16 @@ void Simulator::addPathsToAgents() { //city unsigned int dartDistForPath = 50 ; - for (unsigned int i = 0 ; i < agents_.size() ; ++i) { + for (unsigned int i = 0; i < agents_.size(); ++i) + { agents_[i]->goals_.clear() ; Dart dStart = agents_[i]->part_.d ; Dart dStop = dStart ; - for (unsigned int j = 0 ; /*!envMap_.pedWayMark.isMarked(dStop) ||*/ - envMap_.buildingMark.isMarked(dStop) || j < dartDistForPath + rand() * 20 - || envMap_.map.sameFace(dStop, dStart) ; ++j) { + for (unsigned int j = 0; /*!envMap_.pedWayMark.isMarked(dStop) ||*/ + envMap_.buildingMark.isMarked(dStop) || j < dartDistForPath + rand() * 20 + || envMap_.map.sameFace(dStop, dStart); ++j) + { envMap_.map.next(dStop) ; if (dStop == envMap_.map.end()) dStop = envMap_.map.begin() ; } @@ -506,11 +531,14 @@ void Simulator::addPathsToAgents() // std::cout << "dest1" << Algo::Geometry::faceCentroid(envMap_.map, dStop, envMap_.position) << std::endl; std::vector path = CGoGN::PathFinder::pathFindAStar(envMap_.map, - envMap_.position, dStart, dStop, envMap_.buildingMark) ; + envMap_.position, dStart, + dStop, + envMap_.buildingMark) ; - for (std::vector::iterator it = path.begin() ; it != path.end() ; ++it) { + for (std::vector::iterator it = path.begin(); it != path.end(); ++it) + { // VEC3 dest = Algo::Geometry::faceCentroid(envMap_.map, *it, envMap_.position); - VEC3 dest = envMap_.position[ *it] + envMap_.position[envMap_.map.phi1( *it)] ; + VEC3 dest = envMap_.position[*it] + envMap_.position[envMap_.map.phi1(*it)] ; dest /= 2.0f ; dest[2] = 0 ; @@ -518,36 +546,38 @@ void Simulator::addPathsToAgents() } Dart dStop2 = dStop ; - for (unsigned int j = 0 ; /*!envMap_.pedWayMark.isMarked(dStop) ||*/ - envMap_.buildingMark.isMarked(dStop2) || j < dartDistForPath + rand() * 20 - || envMap_.map.sameFace(dStop, dStop2) - || envMap_.map.sameFace(dStop2, dStart) ; ++j) { + for (unsigned int j = 0; /*!envMap_.pedWayMark.isMarked(dStop) ||*/ + envMap_.buildingMark.isMarked(dStop2) || j < dartDistForPath + rand() * 20 + || envMap_.map.sameFace(dStop, dStop2) || envMap_.map.sameFace(dStop2, dStart); ++j) + { envMap_.map.next(dStop2) ; if (dStop2 == envMap_.map.end()) dStop2 = envMap_.map.begin() ; } // std::cout << "dest2" << Algo::Geometry::faceCentroid(envMap_.map, dStop2, envMap_.position) << std::endl; - path = CGoGN::PathFinder::pathFindAStar(envMap_.map, envMap_.position, dStop, - dStop2, envMap_.buildingMark) ; + path = CGoGN::PathFinder::pathFindAStar(envMap_.map, envMap_.position, dStop, dStop2, + envMap_.buildingMark) ; - for (std::vector::iterator it = path.begin() ; it != path.end() ; ++it) { + for (std::vector::iterator it = path.begin(); it != path.end(); ++it) + { // VEC3 dest = Algo::Geometry::faceCentroid(envMap_.map, *it, envMap_.position); - VEC3 dest = envMap_.position[ *it] + envMap_.position[envMap_.map.phi1( *it)] ; + VEC3 dest = envMap_.position[*it] + envMap_.position[envMap_.map.phi1(*it)] ; dest /= 2.0f ; dest[2] = 0 ; agents_[i]->goals_.push_back(dest) ; } - path = CGoGN::PathFinder::pathFindAStar(envMap_.map, envMap_.position, dStop2, - dStart, envMap_.buildingMark) ; + path = CGoGN::PathFinder::pathFindAStar(envMap_.map, envMap_.position, dStop2, dStart, + envMap_.buildingMark) ; // std::cout << "destStart" << Algo::Geometry::faceCentroid(envMap_.map, dStart, envMap_.position) << std::endl; - for (std::vector::iterator it = path.begin() ; it != path.end() ; ++it) { + for (std::vector::iterator it = path.begin(); it != path.end(); ++it) + { // VEC3 dest = Algo::Geometry::faceCentroid(envMap_.map, *it, envMap_.position); - VEC3 dest = envMap_.position[ *it] + envMap_.position[envMap_.map.phi1( *it)] ; + VEC3 dest = envMap_.position[*it] + envMap_.position[envMap_.map.phi1(*it)] ; dest /= 2.0f ; dest[2] = 0 ; @@ -560,47 +590,54 @@ void Simulator::addPathsToAgents_height() { //city unsigned int dartDistForPath = 50 ; - for (unsigned int i = 0 ; i < agents_.size() ; ++i) { + for (unsigned int i = 0; i < agents_.size(); ++i) + { agents_[i]->goals_.clear() ; Dart dStart = agents_[i]->part_.d ; Dart dStop = dStart ; - for (unsigned int j = 0 ; - envMap_.buildingMark.isMarked(dStop) || j < dartDistForPath + rand() * 20 - || envMap_.map.sameFace(dStop, dStart) ; ++j) { + for (unsigned int j = 0; + envMap_.buildingMark.isMarked(dStop) || j < dartDistForPath + rand() * 20 + || envMap_.map.sameFace(dStop, dStart); ++j) + { envMap_.map.next(dStop) ; if (dStop == envMap_.map.end()) dStop = envMap_.map.begin() ; } std::vector path = CGoGN::PathFinder::pathFindAStar(envMap_.map, - envMap_.position, dStart, dStop, envMap_.buildingMark) ; + envMap_.position, dStart, + dStop, + envMap_.buildingMark) ; - for (std::vector::iterator it = path.begin() ; it != path.end() ; ++it) { + for (std::vector::iterator it = path.begin(); it != path.end(); ++it) + { VEC3 dest = Algo::Geometry::faceCentroid(envMap_.map, *it, envMap_.position) ; agents_[i]->goals_.push_back(dest) ; } Dart dStop2 = dStop ; - for (unsigned int j = 0 ; - envMap_.buildingMark.isMarked(dStop2) || j < dartDistForPath + rand() * 20 - || envMap_.map.sameFace(dStop, dStop2) - || envMap_.map.sameFace(dStop2, dStart) ; ++j) { + for (unsigned int j = 0; + envMap_.buildingMark.isMarked(dStop2) || j < dartDistForPath + rand() * 20 + || envMap_.map.sameFace(dStop, dStop2) || envMap_.map.sameFace(dStop2, dStart); ++j) + { envMap_.map.next(dStop2) ; if (dStop2 == envMap_.map.end()) dStop2 = envMap_.map.begin() ; } - path = CGoGN::PathFinder::pathFindAStar(envMap_.map, envMap_.position, dStop, - dStop2, envMap_.buildingMark) ; + path = CGoGN::PathFinder::pathFindAStar(envMap_.map, envMap_.position, dStop, dStop2, + envMap_.buildingMark) ; - for (std::vector::iterator it = path.begin() ; it != path.end() ; ++it) { + for (std::vector::iterator it = path.begin(); it != path.end(); ++it) + { VEC3 dest = Algo::Geometry::faceCentroid(envMap_.map, *it, envMap_.position) ; agents_[i]->goals_.push_back(dest) ; } - path = CGoGN::PathFinder::pathFindAStar(envMap_.map, envMap_.position, dStop2, - dStart, envMap_.buildingMark) ; + path = CGoGN::PathFinder::pathFindAStar(envMap_.map, envMap_.position, dStop2, dStart, + envMap_.buildingMark) ; - for (std::vector::iterator it = path.begin() ; it != path.end() ; ++it) { + for (std::vector::iterator it = path.begin(); it != path.end(); ++it) + { VEC3 dest = Algo::Geometry::faceCentroid(envMap_.map, *it, envMap_.position) ; agents_[i]->goals_.push_back(dest) ; } @@ -612,18 +649,21 @@ bool Simulator::importAgents(std::string filename) { std::ifstream myfile(filename.c_str(), std::ios::in) ; - if ( !myfile.good()) { + if (!myfile.good()) + { std::cerr << "(export) Unable to open file " << filename << std::endl ; return false ; } - std::string line , token ; - while (myfile.good()) { + std::string line, token ; + while (myfile.good()) + { getline(myfile, line) ; - if (line.size() > 1) { + if (line.size() > 1) + { std::istringstream iss(line) ; - float x , y , z ; + float x, y, z ; iss >> x ; iss >> y ; @@ -651,7 +691,7 @@ bool Simulator::importAgents(std::string filename) agents_.push_back(new Agent(this, pos, dCell)) ; #endif agents_.back()->goals_.push_back(pos) ; - agents_.back()->goals_.push_back( -1.0f * pos) ; + agents_.back()->goals_.push_back(-1.0f * pos) ; agents_.back()->curGoal_ = 1 ; } } @@ -659,7 +699,7 @@ bool Simulator::importAgents(std::string filename) swapAgentsGoals() ; #ifndef SPATIAL_HASHING - for (unsigned int i = 0 ; i < agents_.size() ; ++i) + for (unsigned int i = 0; i < agents_.size(); ++i) envMap_.pushAgentInCells(agents_[i], agents_[i]->part_.d) ; #endif @@ -670,12 +710,13 @@ bool Simulator::importAgents(std::string filename) bool Simulator::exportAgents(std::string filename) { std::ofstream out(filename.c_str(), std::ios::out) ; - if ( !out.good()) { + if (!out.good()) + { std::cerr << "(export) Unable to open file " << filename << std::endl ; return false ; } - for (unsigned int i = 0 ; i < agents_.size() ; ++i) + for (unsigned int i = 0; i < agents_.size(); ++i) #ifdef SPATIAL_HASHING out << agents_[i]->pos << std::endl ; #else @@ -689,7 +730,8 @@ bool Simulator::exportAgents(std::string filename) void Simulator::swapAgentsGoals() { unsigned int nbAgents = agents_.size() ; - for (unsigned int i = 0 ; i < nbAgents ; ++i) { + for (unsigned int i = 0; i < nbAgents; ++i) + { unsigned int r = rand() % nbAgents ; // for(unsigned int nbg = 0; nbg < agents_[i]->goals_.size(); ++nbg) // { @@ -706,7 +748,8 @@ Geom::BoundingBox Simulator::getAgentsBB() { Geom::BoundingBox bb ; unsigned int nbAgents = agents_.size() ; - for (unsigned int i = 0 ; i < nbAgents ; ++i) { + for (unsigned int i = 0; i < nbAgents; ++i) + { bb.addPoint(agents_[i]->getPosition()) ; } diff --git a/src/viewer.cpp b/src/viewer.cpp index e1287be8ca59362b880ec930b0ba6a4e2bcde0a4..989f400abb3732c2840e04214e522e9094145aed 100644 --- a/src/viewer.cpp +++ b/src/viewer.cpp @@ -25,42 +25,49 @@ #include "viewer.h" #include "env_generator.h" #include -#include -SocialAgents::SocialAgents() : - drawEnvLines(true), drawEnvFaces(false), drawEnvTopo(false), drawObstacles(true), drawAgents( - true), drawAgentsNeighborDist(false), drawAgentsObstacleDist(false), drawAgentsPredictionTri( - false) +SocialAgents::SocialAgents(int minSize,int iterations) : + nbIterations(0), + maxIterations(iterations), + frames(0), + sim(minSize), + render_anim(false), + drawEnvLines(true), + drawEnvFaces(false), + drawEnvTopo(false), + drawObstacles(true), + drawAgents(true), + drawAgentsNeighborDist(false), + drawAgentsObstacleDist(false), + drawAgentsPredictionTri(false) { - render_anim = false ; - nbGenerated = 0 ; + timer = new QTimer(this) ; + connect(timer, SIGNAL(timeout()), this, SLOT(animate())) ; - displayFps = true ; nextUpdate = clock() ; - fps = 0 ; glEnable(GL_POINT_SMOOTH) ; - - timer = new QTimer(this) ; - connect(timer, SIGNAL(timeout()), this, SLOT(animate())) ; } void SocialAgents::initGUI() { - setDock( &dock) ; + setDock(&dock) ; dock.check_drawEnvLines->setChecked(true) ; dock.check_drawAgents->setChecked(true) ; -setCallBack( dock.check_timer, SIGNAL(toggled(bool)), SLOT(slot_timer(bool)) ) ; -setCallBack( dock.check_drawEnvLines, SIGNAL(toggled(bool)), SLOT(slot_drawEnvLines(bool)) ) ; -setCallBack( dock.check_drawEnvFaces, SIGNAL(toggled(bool)), SLOT(slot_drawEnvFaces(bool)) ) ; -setCallBack( dock.check_drawEnvTopo, SIGNAL(toggled(bool)), SLOT(slot_drawEnvTopo(bool)) ) ; -setCallBack( dock.check_drawObstacles, SIGNAL(toggled(bool)), SLOT(slot_drawObstacles(bool)) ) ; -setCallBack( dock.check_drawAgents, SIGNAL(toggled(bool)), SLOT(slot_drawAgents(bool)) ) ; -setCallBack( dock.check_drawAgentsPredictionTri, SIGNAL(toggled(bool)), SLOT(slot_drawAgentsPredictionTri(bool)) ) ; -setCallBack( dock.check_drawAgentsNeighborDist, SIGNAL(toggled(bool)), SLOT(slot_drawAgentsNeighborDist(bool)) ) ; -setCallBack( dock.check_drawAgentsObstacleDist, SIGNAL(toggled(bool)), SLOT(slot_drawAgentsObstacleDist(bool)) ) ; + setCallBack(dock.check_timer, SIGNAL(toggled(bool)), SLOT(slot_timer(bool))) ; + setCallBack(dock.check_drawEnvLines, SIGNAL(toggled(bool)), SLOT(slot_drawEnvLines(bool))) ; + setCallBack(dock.check_drawEnvFaces, SIGNAL(toggled(bool)), SLOT(slot_drawEnvFaces(bool))) ; + setCallBack(dock.check_drawEnvTopo, SIGNAL(toggled(bool)), SLOT(slot_drawEnvTopo(bool))) ; + setCallBack(dock.check_drawObstacles, SIGNAL(toggled(bool)), SLOT(slot_drawObstacles(bool))) ; + setCallBack(dock.check_drawAgents, SIGNAL(toggled(bool)), SLOT(slot_drawAgents(bool))) ; + setCallBack(dock.check_drawAgentsPredictionTri, SIGNAL(toggled(bool)), + SLOT(slot_drawAgentsPredictionTri(bool))) ; + setCallBack(dock.check_drawAgentsNeighborDist, SIGNAL(toggled(bool)), + SLOT(slot_drawAgentsNeighborDist(bool))) ; + setCallBack(dock.check_drawAgentsObstacleDist, SIGNAL(toggled(bool)), + SLOT(slot_drawAgentsObstacleDist(bool))) ; } void SocialAgents::cb_initGL() @@ -68,8 +75,8 @@ void SocialAgents::cb_initGL() Utils::GLSLShader::setCurrentOGLVersion(1) ; setFocal(5.0f) ; - Geom::BoundingBox bb = Algo::Geometry::computeBoundingBox(sim.envMap_.map, - sim.envMap_.position) ; + Geom::BoundingBox bb ; + bb = Algo::Geometry::computeBoundingBox(sim.envMap_.map, sim.envMap_.position) ; VEC3 gPosObj = bb.center() ; float tailleX = bb.size(0) ; float tailleY = bb.size(1) ; @@ -115,99 +122,104 @@ void SocialAgents::cb_redraw() // glEnd() ; //#endif - if (drawAgents) { - for (std::vector::iterator it = sim.agents_.begin() ; it != sim.agents_.end() ; - ++it) { + if (drawAgents) + { + for (std::vector::iterator it = sim.agents_.begin(); it != sim.agents_.end(); ++it) + { glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) ; renderAgent(sim.envMap_, *it, drawAgentsNeighborDist, drawAgentsObstacleDist) ; -// for(std::vector >::iterator obst = (*it)->obstacleNeighbors_.begin(); -// obst != (*it)->obstacleNeighbors_.end() && obst->first < (*it)->rangeSq_; -// ++obst) +// for (std::vector >::iterator obst = (*it)->obstacleNeighbors_.begin(); +// obst != (*it)->obstacleNeighbors_.end() && obst->first < (*it)->rangeSq_; ++obst) // { -// glColor3f(0.0f, 1.0f, 1.0f); -// glLineWidth(10.0f); -// renderDart(sim.envMap_, obst->second); +// glColor3f(0.0f, 1.0f, 1.0f) ; +// glLineWidth(10.0f) ; +// renderDart(sim.envMap_, obst->second) ; // } } -// CellMarker m(sim.envMap_.map, FACE); -// for(Dart d = sim.envMap_.map.begin() ; d != sim.envMap_.map.end() ; sim.envMap_.map.next(d)) +// CellMarker m(sim.envMap_.map, FACE) ; +// for (Dart d = sim.envMap_.map.begin(); d != sim.envMap_.map.end(); sim.envMap_.map.next(d)) // { -// if(!m.isMarked(d) && !sim.envMap_.buildingMark.isMarked(d)) +// if (!m.isMarked(d) && !sim.envMap_.buildingMark.isMarked(d)) // { -// m.mark(d); +// m.mark(d) ; // -// Dart dd = d; -// VEC3 cornerLeftBottom = sim.envMap_.position[d]; +// Dart dd = d ; +// VEC3 cornerLeftBottom = sim.envMap_.position[d] ; // do // { -// VEC3 p = sim.envMap_.position[dd]; -// if(cornerLeftBottom[0] > p[0]) -// cornerLeftBottom[0] = p[0]; -// if(cornerLeftBottom[1] > p[1]) -// cornerLeftBottom[1] = p[1]; -// if(cornerLeftBottom[2] > p[2]) -// cornerLeftBottom[2] = p[2]; -// dd = sim.envMap_.map.phi1(dd); -// } while (dd != d); +// VEC3 p = sim.envMap_.position[dd] ; +// if (cornerLeftBottom[0] > p[0]) cornerLeftBottom[0] = p[0] ; +// if (cornerLeftBottom[1] > p[1]) cornerLeftBottom[1] = p[1] ; +// if (cornerLeftBottom[2] > p[2]) cornerLeftBottom[2] = p[2] ; +// dd = sim.envMap_.map.phi1(dd) ; +// } while (dd != d) ; // // //draw vectors // //contains -// for(unsigned int i = 0 ; i < sim.envMap_.agentvect[d].size() ; ++i) +// for (unsigned int i = 0; i < sim.envMap_.agentvect[d].size(); ++i) // { -// Agent* ag = sim.envMap_.agentvect[d][i]; +// Agent* ag = sim.envMap_.agentvect[d][i] ; // -// VEC3 col = Utils::color_map_BCGYR(float(ag->agentNo)/float(sim.agents_.size())); -// glColor3fv(col.data()); -// glBegin(GL_POINTS); -// if(i < 5) -// glVertex3f(cornerLeftBottom[0]+5+1+i*2, cornerLeftBottom[1]+7+1.5, cornerLeftBottom[2]+2); +// VEC3 col = Utils::color_map_BCGYR( +// float(ag->agentNo) / float(sim.agents_.size())) ; +// glColor3fv(col.data()) ; +// glBegin(GL_POINTS) ; +// if (i < 5) +// glVertex3f(cornerLeftBottom[0] + 5 + 1 + i * 2, +// cornerLeftBottom[1] + 7 + 1.5, cornerLeftBottom[2] + 2) ; // else -// glVertex3f(cornerLeftBottom[0]+5+1+(i-5)*2, cornerLeftBottom[1]+7+1.5+3, cornerLeftBottom[2]+2); -// glEnd(); +// glVertex3f(cornerLeftBottom[0] + 5 + 1 + (i - 5) * 2, +// cornerLeftBottom[1] + 7 + 1.5 + 3, cornerLeftBottom[2] + 2) ; +// glEnd() ; // } // // //neighbours -// for(unsigned int i = 0 ; i < sim.envMap_.neighborAgentvect[d].size() ; ++i) +// for (unsigned int i = 0; i < sim.envMap_.neighborAgentvect[d].size(); ++i) // { -// Agent* ag = sim.envMap_.neighborAgentvect[d][i]; +// Agent* ag = sim.envMap_.neighborAgentvect[d][i] ; // -// VEC3 col = Utils::color_map_BCGYR(float(ag->agentNo)/float(sim.agents_.size())); -// glColor3fv(col.data()); -// glBegin(GL_POINTS); -// if(i < 8) -// glVertex3f(12+cornerLeftBottom[0]+5+1+i*2, cornerLeftBottom[1]+7+1.5, cornerLeftBottom[2]+2); +// VEC3 col = Utils::color_map_BCGYR( +// float(ag->agentNo) / float(sim.agents_.size())) ; +// glColor3fv(col.data()) ; +// glBegin(GL_POINTS) ; +// if (i < 8) +// glVertex3f(12 + cornerLeftBottom[0] + 5 + 1 + i * 2, +// cornerLeftBottom[1] + 7 + 1.5, cornerLeftBottom[2] + 2) ; // else -// glVertex3f(12+cornerLeftBottom[0]+5+1+(i-8)*2, cornerLeftBottom[1]+7+1.5+3, cornerLeftBottom[2]+2); -// glEnd(); +// glVertex3f(12 + cornerLeftBottom[0] + 5 + 1 + (i - 8) * 2, +// cornerLeftBottom[1] + 7 + 1.5 + 3, cornerLeftBottom[2] + 2) ; +// glEnd() ; // } // } // } } #ifndef SPATIAL_HASHING - if (drawAgentsPredictionTri) { - for (std::vector::iterator it = sim.agents_.begin() ; it != sim.agents_.end() ; - ++it) { - glColor3f(( *it)->part_.state / 3.0f, ( *it)->part_.state % 2, 0.0f) ; + if (drawAgentsPredictionTri) + { + for (std::vector::iterator it = sim.agents_.begin(); it != sim.agents_.end(); ++it) + { + glColor3f((*it)->part_.state / 3.0f, (*it)->part_.state % 2, 0.0f) ; glLineWidth(5.0f) ; - renderPredictionTriangle(sim.envMap_, ( *it)->part_.d, ( *it)->getPosition()) ; + renderPredictionTriangle(sim.envMap_, (*it)->part_.d, (*it)->getPosition()) ; } } #endif //draw the environment - if (drawEnvLines) { + if (drawEnvLines) + { glDisable(GL_LIGHTING) ; glColor3f(1.0f, 1.0f, 1.0f) ; glLineWidth(1.0f) ; #ifdef SPATIAL_HASHING glBegin(GL_LINES) ; - for(unsigned int i = 0 ; i < sim.envMap_.a_cell_nb_x ; ++i) + for(unsigned int i = 0 ; i < sim.envMap_.agentGridSize(0) ; ++i) { - for(unsigned int j = 0 ; j < sim.envMap_.a_cell_nb_y ; ++j) + for(unsigned int j = 0 ; j < sim.envMap_.agentGridSize(1) ; ++j) { Geom::Vec2ui c(i, j) ; if(sim.envMap_.ht_agents.hasData(c)) @@ -215,7 +227,7 @@ void SocialAgents::cb_redraw() float x = sim.envMap_.geometry.min()[0] ; x += (float) i * (float) sim.envMap_.minCellSize ; float y = sim.envMap_.geometry.min()[1] ; - y += (float) j * (float) sim.envMap_.minCellSize; + y += (float) j * (float) sim.envMap_.minCellSize ; glVertex3f(x, y, 0.0f) ; x += sim.envMap_.minCellSize ; @@ -236,36 +248,44 @@ void SocialAgents::cb_redraw() #else glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) ; Algo::Render::GL1::renderTriQuadPoly(sim.envMap_.map, Algo::Render::GL1::LINE, 1.0, - sim.envMap_.position, sim.envMap_.normal) ; -// Algo::Render::GL1::renderTriQuadPoly(sim.envMap_.mapScenary, Algo::Render::GL1::LINE, 1.0, sim.envMap_.positionScenary, sim.envMap_.normalScenary); + sim.envMap_.position, sim.envMap_.normal) ; +// Algo::Render::GL1::renderTriQuadPoly(sim.envMap_.mapScenary, Algo::Render::GL1::LINE, +// 1.0, sim.envMap_.positionScenary, +// sim.envMap_.normalScenary) ; #endif - } +} - if (drawEnvFaces) { + if (drawEnvFaces) + { glEnable(GL_LIGHTING) ; glColor3f(1.0f, 1.0f, 1.0f) ; glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) ; glEnable(GL_POLYGON_OFFSET_FILL) ; glPolygonOffset(1.0f, 1.0f) ; Algo::Render::GL1::renderTriQuadPoly(sim.envMap_.map, Algo::Render::GL1::LINE, 1.0, - sim.envMap_.position, sim.envMap_.normal) ; -// Algo::Render::GL1::renderTriQuadPoly(sim.envMap_.mapScenary, Algo::Render::GL1::LINE, 1.0, sim.envMap_.positionScenary, sim.envMap_.normalScenary); + sim.envMap_.position, sim.envMap_.normal) ; +// Algo::Render::GL1::renderTriQuadPoly(sim.envMap_.mapScenary, Algo::Render::GL1::LINE, +// 1.0, sim.envMap_.positionScenary, +// sim.envMap_.normalScenary) ; } - if (drawEnvTopo) { + if (drawEnvTopo) + { glDisable(GL_LIGHTING) ; glLineWidth(1.0f) ; Algo::Render::GL1::renderTopoMD2(sim.envMap_.map, sim.envMap_.position, true, true, - 0.9f, 0.9f) ; + 0.9f, 0.9f) ; } - if (drawObstacles) { + if (drawObstacles) + { glColor3f(1.0f, 0.0f, 0.0f) ; glLineWidth(5.0f) ; CellMarker dmo(sim.envMap_.map, EDGE) ; - for (Dart d = sim.envMap_.map.begin() ; d != sim.envMap_.map.end() ; - sim.envMap_.map.next(d)) { - if ( !dmo.isMarked(d)) { + for (Dart d = sim.envMap_.map.begin(); d != sim.envMap_.map.end(); sim.envMap_.map.next(d)) + { + if (!dmo.isMarked(d)) + { dmo.mark(d) ; if (sim.envMap_.obstacleMark.isMarked(d)) renderDart(sim.envMap_, d) ; } @@ -274,9 +294,9 @@ void SocialAgents::cb_redraw() #ifdef SPATIAL_HASHING glLineWidth(1.0f) ; glBegin(GL_LINES) ; - for(unsigned int i = 0 ; i < sim.envMap_.o_cell_nb_x ; ++i) + for(unsigned int i = 0 ; i < sim.envMap_.obstacleGridSize(0) ; ++i) { - for(unsigned int j = 0 ; j < sim.envMap_.o_cell_nb_y ; ++j) + for(unsigned int j = 0 ; j < sim.envMap_.obstacleGridSize(1) ; ++j) { Geom::Vec2ui c(i, j) ; if(sim.envMap_.ht_obstacles.hasData(c)) @@ -301,233 +321,261 @@ void SocialAgents::cb_redraw() glEnd() ; #endif -// glColor3f(1.0f, 0.0f, 1.0f); -// for(std::vector::iterator it = sim.movingObstacles_.begin() ; it != sim.movingObstacles_.end() ; ++it) +// glColor3f(1.0f, 0.0f, 1.0f) ; +// for (std::vector::iterator it = sim.movingObstacles_.begin(); +// it != sim.movingObstacles_.end(); ++it) // { -// glBegin(GL_LINE_LOOP); -// for(unsigned int i = 0; i < ((*it)->nbVertices); ++i) +// glBegin(GL_LINE_LOOP) ; +// for (unsigned int i = 0; i < ((*it)->nbVertices); ++i) // { -// glVertex3fv((*it)->obstacles_[i]->p1.data()); +// glVertex3fv((*it)->obstacles_[i]->p1.data()) ; // } -// glEnd(); +// glEnd() ; // } } -// renderDart(sim.envMap_,264); - -// glPushMatrix(); -// glBegin(GL_LINES); -// unsigned int iter = 100; -// for(int i=0;i<700;i=i+iter) +// renderDart(sim.envMap_, 264) ; +// +// glPushMatrix() ; +// glBegin(GL_LINES) ; +// unsigned int iter = 100 ; +// for (int i = 0; i < 700; i = i + iter) // { -// VEC3 camPos(-800,-800,10); -// camPos = camPos + VEC3(-10-0.3*i,400.0f*log(1.0f+i/700.0f),30+0.3*i); -// glVertex3fv(&camPos[0]); -// camPos = VEC3(-800,-800,10); -// camPos = camPos + VEC3(-10-0.3*(i+iter),400.0f*log(1.0f+(i+iter)/700.0f),30+0.3*(i+iter)); -// glVertex3fv(&camPos[0]); +// VEC3 camPos(-800, -800, 10) ; +// camPos = camPos + VEC3(-10 - 0.3 * i, 400.0f * log(1.0f + i / 700.0f), 30 + 0.3 * i) ; +// glVertex3fv(&camPos[0]) ; +// camPos = VEC3(-800, -800, 10) ; +// camPos = camPos +// + VEC3(-10 - 0.3 * (i + iter), 400.0f * log(1.0f + (i + iter) / 700.0f), +// 30 + 0.3 * (i + iter)) ; +// glVertex3fv(&camPos[0]) ; // } -// glEnd(); -// glPopMatrix(); +// glEnd() ; +// glPopMatrix() ; // -// glPushMatrix(); -// glBegin(GL_LINES); -// for(int i=0;i<700;i=i+iter) +// glPushMatrix() ; +// glBegin(GL_LINES) ; +// for (int i = 0; i < 700; i = i + iter) // { -// VEC3 camLook(0,0,0); -// camLook = camLook + VEC3(-10-0.1*i,400.0f*log(1.0f+i/700.0f),30+0.1*i); -// glVertex3fv(&camLook[0]); -// camLook = VEC3(0,0,0); -// camLook = camLook + VEC3(-10-0.1*(i+iter),400.0f*log(1.0f+(i+iter)/700.0f),30+0.1*(i+iter)); -// glVertex3fv(&camLook[0]); +// VEC3 camLook(0, 0, 0) ; +// camLook = camLook + VEC3(-10 - 0.1 * i, 400.0f * log(1.0f + i / 700.0f), 30 + 0.1 * i) ; +// glVertex3fv(&camLook[0]) ; +// camLook = VEC3(0, 0, 0) ; +// camLook = camLook +// + VEC3(-10 - 0.1 * (i + iter), 400.0f * log(1.0f + (i + iter) / 700.0f), +// 30 + 0.1 * (i + iter)) ; +// glVertex3fv(&camLook[0]) ; // } -// glEnd(); -// glPopMatrix(); - -// glPushMatrix(); -// glBegin(GL_LINES); -// for(int i=0;i<700;i=i+iter) +// glEnd() ; +// glPopMatrix() ; +// +// glPushMatrix() ; +// glBegin(GL_LINES) ; +// for (int i = 0; i < 700; i = i + iter) // { -// VEC3 camPos(0,-200,10); -// camPos = camPos + VEC3(-10-0.15*i,0,0); -// glVertex3fv(&camPos[0]); -// camPos = camPos + VEC3(-10-0.15*(i+iter),0,0); -// glVertex3fv(&camPos[0]); +// VEC3 camPos(0, -200, 10) ; +// camPos = camPos + VEC3(-10 - 0.15 * i, 0, 0) ; +// glVertex3fv(&camPos[0]) ; +// camPos = camPos + VEC3(-10 - 0.15 * (i + iter), 0, 0) ; +// glVertex3fv(&camPos[0]) ; // } -// glEnd(); -// glPopMatrix(); +// glEnd() ; +// glPopMatrix() ; // -// glPushMatrix(); -// glBegin(GL_LINES); -// for(int i=0;i<700;i=i+iter) +// glPushMatrix() ; +// glBegin(GL_LINES) ; +// for (int i = 0; i < 700; i = i + iter) // { -// VEC3 camLook(0,-200,10); -// camLook[1] += 100; -// camLook = camLook + VEC3(-10-0.15*i,0,0); -// glVertex3fv(&camLook[0]); -// camLook = camLook + VEC3(-10-0.15*(i+iter),0,0); -// glVertex3fv(&camLook[0]); +// VEC3 camLook(0, -200, 10) ; +// camLook[1] += 100 ; +// camLook = camLook + VEC3(-10 - 0.15 * i, 0, 0) ; +// glVertex3fv(&camLook[0]) ; +// camLook = camLook + VEC3(-10 - 0.15 * (i + iter), 0, 0) ; +// glVertex3fv(&camLook[0]) ; // } -// glEnd(); -// glPopMatrix(); - -// glColor3f(0.0f, 1.0f, 0.0f); -// glLineWidth(5.0f); -// CellMarker dmb(sim.envMap_.map, FACE); -// for(Dart d = sim.envMap_.map.begin(); d != sim.envMap_.map.end(); sim.envMap_.map.next(d)) +// glEnd() ; +// glPopMatrix() ; +// +// glColor3f(0.0f, 1.0f, 0.0f) ; +// glLineWidth(5.0f) ; +// CellMarker dmb(sim.envMap_.map, FACE) ; +// for (Dart d = sim.envMap_.map.begin(); d != sim.envMap_.map.end(); sim.envMap_.map.next(d)) // { -// if(!dmb.isMarked(d)) +// if (!dmb.isMarked(d)) // { -// dmb.mark(d); -// if(sim.envMap_.buildingMark.isMarked(d)) +// dmb.mark(d) ; +// if (sim.envMap_.buildingMark.isMarked(d)) // { -// VEC3 c = Algo::Geometry::faceCentroid(sim.envMap_.map, d, sim.envMap_.position); -// glBegin(GL_LINES); -// Dart dd = d; +// VEC3 c = Algo::Geometry::faceCentroid(sim.envMap_.map, d, +// sim.envMap_.position) ; +// glBegin(GL_LINES) ; +// Dart dd = d ; // do // { -// VEC3 p1 = sim.envMap_.position[dd]; -// VEC3 p2 = sim.envMap_.position[sim.envMap_.map.phi1(dd)]; -// VEC3 vec = c - p1; -// p1 = p1 + vec*0.25f; -// vec = c - p2; -// p2 = p2 + vec*0.25f; +// VEC3 p1 = sim.envMap_.position[dd] ; +// VEC3 p2 = sim.envMap_.position[sim.envMap_.map.phi1(dd)] ; +// VEC3 vec = c - p1 ; +// p1 = p1 + vec * 0.25f ; +// vec = c - p2 ; +// p2 = p2 + vec * 0.25f ; // -// glVertex3f(p1[0],p1[1],p1[2]); -// glVertex3f(p2[0],p2[1],p2[2]); +// glVertex3f(p1[0], p1[1], p1[2]) ; +// glVertex3f(p2[0], p2[1], p2[2]) ; // -// dd = sim.envMap_.map.phi1(dd); -// } while(dd != d); -// glEnd(); +// dd = sim.envMap_.map.phi1(dd) ; +// } while (dd != d) ; +// glEnd() ; // } // } // } - if (displayFps) { - ++frames ; - clock_t time = clock() ; - if (time > nextUpdate) { - std::ostringstream oss ; - oss << "Elapsed time : " << time / CLOCKS_PER_SEC ; - oss << " | " << frames << " fps" ; - oss << " | Updates " << sim.nbUpdates ; - oss << " | Near Neighbors " << sim.nearNeighbors << " [" << sim.nearNeighbors / sim.nbUpdates << "]"; - oss << " | Neighbors " << sim.totalNeighbors << " [" << sim.totalNeighbors / sim.nbUpdates << "]"; - oss << " | Sorts " << sim.nbSorts << " [" << 100 * sim.nbSorts / sim.nbUpdates << "%]"; - oss << " | To Refine " << sim.nbRefineCandidate << " [" << 100 * sim.nbRefineCandidate / sim.nbUpdates << "%]"; - oss << " | To Coarsen " << sim.nbCoarsenCandidate << " [" << 100 * sim.nbCoarsenCandidate / sim.nbUpdates << "%]"; - sim.nbUpdates = 0 ; - sim.nearNeighbors = 0 ; - sim.totalNeighbors = 0 ; - sim.nbSorts = 0 ; - sim.nbRefineCandidate = 0 ; - sim.nbCoarsenCandidate = 0 ; - glColor3f(1.0f, 1.0f, 1.0f) ; - statusMsg(oss.str().c_str()) ; - frames = 0 ; - nextUpdate = time + CLOCKS_PER_SEC ; - } + ++frames ; + clock_t time = clock() ; + if (time > nextUpdate) + { + // Sortie des stats pour analyse externe + std::cout << time / CLOCKS_PER_SEC << ";" << frames << ";" << sim.nbUpdates << ";" + << sim.totalNeighbors << ";" << sim.nearNeighbors << ";" << sim.nbSorts << ";" + << sim.nbRefineCandidate << ";" << sim.nbCoarsenCandidate << std::endl ; + // Affichage des stats dans la barre d'état + std::ostringstream oss ; + oss << "Elapsed time : " << time / CLOCKS_PER_SEC ; + oss << " | " << frames << " fps" ; + oss << " | Iterations " << nbIterations ; + oss << " | Neighbors " << sim.totalNeighbors << " [" << sim.totalNeighbors / sim.nbUpdates + << "]" ; + oss << " | Near Neighbors " << sim.nearNeighbors << " [" + << sim.nearNeighbors / sim.nbUpdates << "]" ; + oss << " | Sorts " << sim.nbSorts << " [" << 100 * sim.nbSorts / sim.nbUpdates << "%]" ; + oss << " | To Refine " << sim.nbRefineCandidate ; + oss << " | To Coarsen " << sim.nbCoarsenCandidate ; + sim.nbUpdates = 0 ; + sim.totalNeighbors = 0 ; + sim.nearNeighbors = 0 ; + sim.nbSorts = 0 ; + sim.nbRefineCandidate = 0 ; + sim.nbCoarsenCandidate = 0 ; + glColor3f(1.0f, 1.0f, 1.0f) ; + statusMsg(oss.str().c_str()) ; + frames = 0 ; + nextUpdate = time + CLOCKS_PER_SEC ; } } void SocialAgents::animate() { -// if(sim.nbSteps_ % sim.nbStepsPerUnit_ == 0 && (sim.nbSteps_ / sim.nbStepsPerUnit_) % 10 == 0) -// std::cout << round(sim.globalTime_) << " " << sim.envMap_.mapMemoryCost() << std::endl; +// if (sim.nbSteps_ % sim.nbStepsPerUnit_ == 0 && (sim.nbSteps_ / sim.nbStepsPerUnit_) % 10 == 0) std::cout +// << round(sim.globalTime_) << " " << sim.envMap_.mapMemoryCost() << std::endl ; +// // CityGenerator::animateCity(&sim.envMap_) ; -// if(CityGenerator::animateCity(&sim.envMap_)) -// sim.addPathToCorner(); -// sim.addPathsToAgents(); - -// std::cout << "t : " << sim.globalTime_ << std::endl; - -// timeval startTime; -// gettimeofday(&startTime, NULL); -// timeval endTime; -// long seconds, nseconds; +// if (CityGenerator::animateCity(&sim.envMap_)) sim.addPathToCorner() ; +// sim.addPathsToAgents() ; +// +// std::cout << "t : " << sim.globalTime_ << std::endl ; +// +// timeval startTime ; +// gettimeofday(&startTime, NULL) ; +// timeval endTime ; +// long seconds, nseconds ; + nbIterations++ ; + if (maxIterations > 0 && nbIterations > maxIterations) + { + timer->stop() ; + exit(0) ; + } sim.doStep() ; -// gettimeofday(&endTime, NULL); -// seconds = endTime.tv_sec - startTime.tv_sec; -// nseconds = endTime.tv_usec - startTime.tv_usec; +// gettimeofday(&endTime, NULL) ; +// seconds = endTime.tv_sec - startTime.tv_sec ; +// nseconds = endTime.tv_usec - startTime.tv_usec ; // -// elapsedTime += (seconds + nseconds/1000000.0) ; - -// if(sim.globalTime_ - int(sim.globalTime_) == 0.0f && int(sim.globalTime_) % 1500 == 0) +// elapsedTime += (seconds + nseconds / 1000000.0) ; +// +// if (sim.globalTime_ - int(sim.globalTime_) == 0.0f && int(sim.globalTime_) % 1500 == 0) // { -// std::cout << "end " << elapsedTime << std::endl; -// timer->stop(); +// std::cout << "end " << elapsedTime << std::endl ; +// timer->stop() ; // } - -// if(sim.reachedGoal()) +// +// if (sim.reachedGoal()) // { -// std::cout << "end " << elapsedTime << std::endl; -// timer->stop(); +// std::cout << "end " << elapsedTime << std::endl ; +// timer->stop() ; // } #ifndef SPATIAL_HASHING - if (render_anim) { + if (render_anim) + { std::ostringstream oss ; std::ostringstream tmpNb ; - tmpNb << std::setfill('0') << std::setw(4) << nbGenerated ; - nbGenerated++ ; + tmpNb << std::setfill('0') << std::setw(4) << nbIterations ; oss << "./pathDyn/dyn" << tmpNb.str() << ".pov" ; std::string chaine = oss.str() ; -// VEC3 agPos = sim.agents_[0]->meanPos_; -// agPos[2] = agPos[1]; -// agPos[1] = 0.0f; -// VEC3 camPos(agPos); -// VEC3 camLook(agPos); -// camPos = camPos+VEC3(-10-0.05*nbGenerated,30+0.05*nbGenerated,log(1.0f+nbGenerated/700.0f)); +// VEC3 agPos = sim.agents_[0]->meanPos_ ; +// agPos[2] = agPos[1] ; +// agPos[1] = 0.0f ; +// VEC3 camPos(agPos) ; +// VEC3 camLook(agPos) ; +// camPos = camPos +// + VEC3(-10 - 0.05 * nbGenerated, 30 + 0.05 * nbGenerated, +// log(1.0f + nbGenerated / 700.0f)) ; //travelling city nbMaxAgent 15000, nbSquare 40 (45?) -// VEC3 camPos(-800,10,-800); -// camPos = camPos + VEC3(-10-0.3*nbGenerated,30+0.3*nbGenerated,400.0f*log(1.0f+nbGenerated/700.0f)); -// VEC3 camLook(0,0,0); -// camLook = camLook + VEC3(-10-0.1*nbGenerated,30+0.1*nbGenerated,400.0f*log(1.0f+nbGenerated/700.0f)); +// VEC3 camPos(-800, 10, -800) ; +// camPos = camPos +// + VEC3(-10 - 0.3 * nbGenerated, 30 + 0.3 * nbGenerated, +// 400.0f * log(1.0f + nbGenerated / 700.0f)) ; +// VEC3 camLook(0, 0, 0) ; +// camLook = camLook +// + VEC3(-10 - 0.1 * nbGenerated, 30 + 0.1 * nbGenerated, +// 400.0f * log(1.0f + nbGenerated / 700.0f)) ; //travelling horizontal -// VEC3 camPos(0,5,-200); -// camPos = camPos + VEC3(-10-0.15*nbGenerated,0,0); -// VEC3 camLook = camPos; -// camLook[0] += 0.15*nbGenerated; -// camLook[2] += 100; -// camLook[1] = 0; +// VEC3 camPos(0, 5, -200) ; +// camPos = camPos + VEC3(-10 - 0.15 * nbGenerated, 0, 0) ; +// VEC3 camLook = camPos ; +// camLook[0] += 0.15 * nbGenerated ; +// camLook[2] += 100 ; +// camLook[1] = 0 ; //vertical view -// VEC3 camPos(0,300,0); -// Geom::BoundingBox bb = sim.getAgentsBB(); -// VEC3 seeEveryone = bb.max()-bb.min(); -// if(seeEveryone[0]>200) -// camPos[1] += seeEveryone[0]; +// VEC3 camPos(0, 300, 0) ; +// Geom::BoundingBox bb = sim.getAgentsBB() ; +// VEC3 seeEveryone = bb.max() - bb.min() ; +// if (seeEveryone[0] > 200) +// camPos[1] += seeEveryone[0] ; // else -// camPos[1] += 200; -// VEC3 camLook(0); +// camPos[1] += 200 ; +// VEC3 camLook(0) ; //video intro -// VEC3 camPos(-20,10,75); -// VEC3 camLook(-25,0,0); +// VEC3 camPos(-20, 10, 75) ; +// VEC3 camLook(-25, 0, 0) ; //mall -// VEC3 camPos(-500,500,-400); -// VEC3 camLook(100,100,0); +// VEC3 camPos(-500, 500, -400) ; +// VEC3 camLook(100, 100, 0) ; //openStreetMap -// VEC3 camPos(500,75,400); -// VEC3 camLook(-250,0,650); +// VEC3 camPos(500, 75, 400) ; +// VEC3 camLook(-250, 0, 650) ; //pathDyn - VEC3 camPos( -700, 700, -1800) ; + VEC3 camPos(-700, 700, -1800) ; VEC3 camLook(200, 0, 0) ; exportScenePov(sim.envMap_.map, sim.envMap_.position, chaine, camPos, camLook, - VEC3(0.0f, 0, 0), 0, 0, 0) ; -// exportScenePov(sim.envMap_.map,sim.envMap_.position,chaine,VEC3(43,762,65),VEC3(0,762,0),VEC3(1.0f,0,0),0,0,0); -// exportScenePov(sim.envMap_.map,sim.envMap_.position,chaine,VEC3(43,762,65+(1500.0f*float(nbGenerated)/400.0f)),VEC3(0,762,0),VEC3(1.0f,0,0),0,0,0); - - if (nbGenerated == 3200) { + VEC3(0.0f, 0, 0), 0, 0, 0) ; +// exportScenePov(sim.envMap_.map, sim.envMap_.position, chaine, VEC3(43, 762, 65), +// VEC3(0, 762, 0), VEC3(1.0f, 0, 0), 0, 0, 0) ; +// exportScenePov(sim.envMap_.map, sim.envMap_.position, chaine, +// VEC3(43, 762, 65 + (1500.0f * float(nbGenerated) / 400.0f)), VEC3(0, 762, 0), +// VEC3(1.0f, 0, 0), 0, 0, 0) ; + + if (nbIterations == 3200) + { std::cout << "enough .pov generated" << std::endl ; exit(0) ; } @@ -543,7 +591,8 @@ void SocialAgents::exportInfoFace(std::ofstream& out, Dart d) //get corner Dart dd = d ; VEC3 cornerLeftBottom = sim.envMap_.position[d] ; - do { + do + { VEC3 p = sim.envMap_.position[dd] ; if (cornerLeftBottom[0] > p[0]) cornerLeftBottom[0] = p[0] ; if (cornerLeftBottom[1] > p[1]) cornerLeftBottom[1] = p[1] ; @@ -552,32 +601,36 @@ void SocialAgents::exportInfoFace(std::ofstream& out, Dart d) } while (dd != d) ; //draw text - if ( !sim.envMap_.buildingMark.isMarked(d)) { + if (!sim.envMap_.buildingMark.isMarked(d)) + { out << "object {" << std::endl ; out << "polygon { 4 , <0,0,0>, <11,0,0>, <11,0,6>, <0,0,6> }" << std::endl ; out << "texture { pigment{ color rgb<0,1,1> } finish { ambient 1 diffuse 0 } }" - << std::endl ; - out << "translate < " << cornerLeftBottom[0] + 5 << "," << cornerLeftBottom[2] + 2 - << "," << cornerLeftBottom[1] + 7 << ">" << std::endl ; + << std::endl ; + out << "translate < " << cornerLeftBottom[0] + 5 << "," << cornerLeftBottom[2] + 2 << "," + << cornerLeftBottom[1] + 7 << ">" << std::endl ; out << "}" << std::endl ; out << "object {" << std::endl ; out << "polygon { 4 , <0,0,0>, <17,0,0>, <17,0,6>, <0,0,6> }" << std::endl ; out << "texture { pigment{ color rgb<0.25,1,0> } finish { ambient 1 diffuse 0 } }" - << std::endl ; + << std::endl ; out << "translate < " << 12 + cornerLeftBottom[0] + 5 << "," << cornerLeftBottom[2] + 2 - << "," << cornerLeftBottom[1] + 7 << ">" << std::endl ; + << "," << cornerLeftBottom[1] + 7 << ">" << std::endl ; out << "}" << std::endl ; } //draw vectors //contains - for (unsigned int i = 0 ; i < sim.envMap_.agentvect[d].size() ; ++i) { + for (unsigned int i = 0; i < sim.envMap_.agentvect[d].size(); ++i) + { Agent * ag = sim.envMap_.agentvect[d][i] ; int j = 0 ; - for (unsigned int iA = 0 ; iA < sim.agents_.size() ; ++iA) { - if (sim.agents_[iA] == ag) { + for (unsigned int iA = 0; iA < sim.agents_.size(); ++iA) + { + if (sim.agents_[iA] == ag) + { j = iA ; break ; } @@ -589,49 +642,57 @@ void SocialAgents::exportInfoFace(std::ofstream& out, Dart d) VEC3 col = Utils::color_map_BCGYR(float(j) / float(sim.agents_.size())) ; out << "pigment { color <" << col[0] << "," << col[1] << "," << col[2] << "> }" - << std::endl ; - -// float col = float(j)/float(sim.agents_.size()); -// col = col*16777215; -// out << "pigment { color <" << (int(col/256/256)%256)/256.0f << "," << (int(col/256)%256)/256.0f << "," << (int(col)%256)/256.0f << "> }" << std::endl; + << std::endl ; -// VEC3 pBase(ag->getPosition()); -// VEC3 posR(pBase); +// float col = float(j) / float(sim.agents_.size()) ; +// col = col * 16777215 ; +// out << "pigment { color <" << (int(col / 256 / 256) % 256) / 256.0f << "," +// << (int(col / 256) % 256) / 256.0f << "," << (int(col) % 256) / 256.0f << "> }" +// << std::endl ; // -// Geom::Plane3D pl = Algo::Geometry::facePlane(sim.envMap_.map,ag->part_.d,sim.envMap_.position); -// posR[2] -= 1000; -// Geom::intersectionPlaneRay(pl,posR,VEC3(0,0,-1),posR); +// VEC3 pBase(ag->getPosition()) ; +// VEC3 posR(pBase) ; // - // VEC3 dir = ag->velocity_; -// VEC3 dir = ag->meanSpeed_; -// dir.normalize(); -// VEC3 base(0,-1,0); -// VEC3 axisRot = base^dir; -// int sign = axisRot[2] > 0 ? 1 : -1; +// Geom::Plane3D pl = Algo::Geometry::facePlane(sim.envMap_.map, ag->part_.d, +// sim.envMap_.position) ; +// posR[2] -= 1000 ; +// Geom::intersectionPlaneRay(pl, posR, VEC3(0, 0, -1), posR) ; // - //57,2957795 : conversion from radian to degree -// float myRot = acos(-dir[1])*57.2957795f; +// VEC3 dir = ag->velocity_ ; +// VEC3 dir = ag->meanSpeed_ ; +// dir.normalize() ; +// VEC3 base(0, -1, 0) ; +// VEC3 axisRot = base ^ dir ; +// int sign = axisRot[2] > 0 ? 1 : -1 ; // +// 57, 2957795 +// : conversion from radian to degree +// float myRot = acos(-dir[1])*57.2957795f ; // -// out << "rotate <0,0," << sign*myRot << ">"<< std::endl; -// out << "rotate <" << -90 << "," << 0 << "," << 0 << "> " << std::endl; - if (i < 5) out << "translate < " << cornerLeftBottom[0] + 5 + 1 + i * 2 << "," - << cornerLeftBottom[2] + 2 << "," << cornerLeftBottom[1] + 7 + 1.5 << ">" - << std::endl ; - else out << "translate < " << cornerLeftBottom[0] + 5 + 1 + (i - 5) * 2 << "," - << cornerLeftBottom[2] + 2 << "," << cornerLeftBottom[1] + 7 + 1.5 + 3 << ">" - << std::endl ; +// out << "rotate <0,0," << sign * myRot << ">" << std::endl ; +// out << "rotate <" << -90 << "," << 0 << "," << 0 << "> " << std::endl ; + if (i < 5) + out << "translate < " << cornerLeftBottom[0] + 5 + 1 + i * 2 << "," + << cornerLeftBottom[2] + 2 << "," << cornerLeftBottom[1] + 7 + 1.5 << ">" + << std::endl ; + else + out << "translate < " << cornerLeftBottom[0] + 5 + 1 + (i - 5) * 2 << "," + << cornerLeftBottom[2] + 2 << "," << cornerLeftBottom[1] + 7 + 1.5 + 3 << ">" + << std::endl ; out << "}" << std::endl ; } //neighbours - for (unsigned int i = 0 ; i < sim.envMap_.neighborAgentvect[d].size() ; ++i) { + for (unsigned int i = 0; i < sim.envMap_.neighborAgentvect[d].size(); ++i) + { Agent * ag = sim.envMap_.neighborAgentvect[d][i] ; int j = 0 ; - for (unsigned int iA = 0 ; iA < sim.agents_.size() ; ++iA) { - if (sim.agents_[iA] == ag) { + for (unsigned int iA = 0; iA < sim.agents_.size(); ++iA) + { + if (sim.agents_[iA] == ag) + { j = iA ; break ; } @@ -643,47 +704,51 @@ void SocialAgents::exportInfoFace(std::ofstream& out, Dart d) VEC3 col = Utils::color_map_BCGYR(float(j) / float(sim.agents_.size())) ; out << "pigment { color <" << col[0] << "," << col[1] << "," << col[2] << "> }" - << std::endl ; - -// float col = float(j)/float(sim.agents_.size()); -// col = col*16777215; -// out << "pigment { color <" << (int(col/256/256)%256)/256.0f << "," << (int(col/256)%256)/256.0f << "," << (int(col)%256)/256.0f << "> }" << std::endl; + << std::endl ; -// VEC3 pBase(ag->getPosition()); -// VEC3 posR(pBase); +// float col = float(j) / float(sim.agents_.size()) ; +// col = col * 16777215 ; +// out << "pigment { color <" << (int(col / 256 / 256) % 256) / 256.0f << "," +// << (int(col / 256) % 256) / 256.0f << "," << (int(col) % 256) / 256.0f << "> }" +// << std::endl ; // -// Geom::Plane3D pl = Algo::Geometry::facePlane(sim.envMap_.map,ag->part_.d,sim.envMap_.position); -// posR[2] -= 1000; -// Geom::intersectionPlaneRay(pl,posR,VEC3(0,0,-1),posR); +// VEC3 pBase(ag->getPosition()) ; +// VEC3 posR(pBase) ; // -// // VEC3 dir = ag->velocity_; -// VEC3 dir = ag->meanSpeed_; -// dir.normalize(); -// VEC3 base(0,-1,0); -// VEC3 axisRot = base^dir; -// int sign = axisRot[2] > 0 ? 1 : -1; +// Geom::Plane3D pl = Algo::Geometry::facePlane(sim.envMap_.map, ag->part_.d, +// sim.envMap_.position) ; +// posR[2] -= 1000 ; +// Geom::intersectionPlaneRay(pl, posR, VEC3(0, 0, -1), posR) ; // -// //57,2957795 : conversion from radian to degree -// float myRot = acos(-dir[1])*57.2957795f; +// // VEC3 dir = ag->velocity_; +// VEC3 dir = ag->meanSpeed_ ; +// dir.normalize() ; +// VEC3 base(0, -1, 0) ; +// VEC3 axisRot = base ^ dir ; +// int sign = axisRot[2] > 0 ? 1 : -1 ; // +// //57,2957795 : conversion from radian to degree +// float myRot = acos(-dir[1]) * 57.2957795f ; // -// out << "rotate <0,0," << sign*myRot << ">"<< std::endl; -// out << "rotate <" << -90 << "," << 0 << "," << 0 << "> " << std::endl; - if (i < 8) out << "translate < " << 12 + cornerLeftBottom[0] + 5 + 1 + i * 2 << "," - << cornerLeftBottom[2] + 2 << "," << cornerLeftBottom[1] + 7 + 1.5 << ">" - << std::endl ; - else out << "translate < " << 12 + cornerLeftBottom[0] + 5 + 1 + (i - 8) * 2 << "," - << cornerLeftBottom[2] + 2 << "," << cornerLeftBottom[1] + 7 + 1.5 + 3 << ">" - << std::endl ; +// out << "rotate <0,0," << sign * myRot << ">" << std::endl ; +// out << "rotate <" << -90 << "," << 0 << "," << 0 << "> " << std::endl ; + if (i < 8) + out << "translate < " << 12 + cornerLeftBottom[0] + 5 + 1 + i * 2 << "," + << cornerLeftBottom[2] + 2 << "," << cornerLeftBottom[1] + 7 + 1.5 << ">" + << std::endl ; + else + out << "translate < " << 12 + cornerLeftBottom[0] + 5 + 1 + (i - 8) * 2 << "," + << cornerLeftBottom[2] + 2 << "," << cornerLeftBottom[1] + 7 + 1.5 + 3 << ">" + << std::endl ; out << "no_shadow }" << std::endl ; } } -bool SocialAgents::exportScenePov(PFP::MAP& map, PFP::TVEC3& position, - const std::string& filename, PFP::VEC3 cameraPos, PFP::VEC3 cameraLook, - PFP::VEC3 translate, float angle_X, float angle_Y, float angle_Z, - const FunctorSelect& good) +bool SocialAgents::exportScenePov(PFP::MAP& map, PFP::TVEC3& position, const std::string& filename, + PFP::VEC3 cameraPos, PFP::VEC3 cameraLook, PFP::VEC3 translate, + float angle_X, float angle_Y, float angle_Z, + const FunctorSelect& good) { static const bool draft = false ; static const bool highlightAgent = false ; @@ -694,7 +759,8 @@ bool SocialAgents::exportScenePov(PFP::MAP& map, PFP::TVEC3& position, unsigned int highlightAgentNo = 0 ; std::ofstream out(filename.c_str(), std::ios::out) ; - if ( !out.good()) { + if (!out.good()) + { std::cerr << "(export) Unable to open file " << filename << std::endl ; return false ; } @@ -704,7 +770,8 @@ bool SocialAgents::exportScenePov(PFP::MAP& map, PFP::TVEC3& position, float angleZ = angle_Z ; //view the agent from an upper position - if (highlightAgent) { + if (highlightAgent) + { Agent * agent = sim.agents_[highlightAgentNo] ; cameraLook = agent->getPosition() ; VEC3 goalV = (agent->goals_[agent->curGoal_] - agent->getPosition()) ; @@ -719,9 +786,9 @@ bool SocialAgents::exportScenePov(PFP::MAP& map, PFP::TVEC3& position, } //define the camera position -// if(draft) -// out << "#include \"sombrero2_lowres_POV_geom.inc\"" << std::endl; - if ( !draft) { +// if (draft) out << "#include \"sombrero2_lowres_POV_geom.inc\"" << std::endl ; + if (!draft) + { out << "#include \"Sheriff_animated_POV_geom.inc\"" << std::endl ; out << "#include \"Cowbow_animated_POV_geom.inc\"" << std::endl ; out << "#include \"sombrero2_highres_POV_geom.inc\"" << std::endl ; @@ -730,20 +797,25 @@ bool SocialAgents::exportScenePov(PFP::MAP& map, PFP::TVEC3& position, out << "#include \"transforms.inc\"" << std::endl ; -// out << "#declare PosSpline = spline { natural_spline 0.0,<0,3000,-1> 0.3,<0,500,-1> 0.6,<250,200,200> 1.0,<500,75,400>}" << std::endl; -// out << "#declare LookSpline = spline { natural_spline 0.0,<0,0,200> 0.3,<-250,0,500> 0.6,<-125,0,350> 1.0,<-250,0,650>}" << std::endl; - -// if((sim.globalTime_-1000.0f)/120.0f<1.0f) -// out << "camera { location PosSpline(" << (sim.globalTime_-1000.0f)/120.0f << ") look_at LookSpline(" << (sim.globalTime_-1000.0f)/120.0f <<") "; +// out +// << "#declare PosSpline = spline { natural_spline 0.0,<0,3000,-1> 0.3,<0,500,-1> 0.6,<250,200,200> 1.0,<500,75,400>}" +// << std::endl ; +// out +// << "#declare LookSpline = spline { natural_spline 0.0,<0,0,200> 0.3,<-250,0,500> 0.6,<-125,0,350> 1.0,<-250,0,650>}" +// << std::endl ; +// +// if ((sim.globalTime_ - 1000.0f) / 120.0f < 1.0f) +// out << "camera { location PosSpline(" << (sim.globalTime_ - 1000.0f) / 120.0f +// << ") look_at LookSpline(" << (sim.globalTime_ - 1000.0f) / 120.0f << ") " ; // else out << "camera { location <" << cameraPos[0] << "," << cameraPos[1] << "," << cameraPos[2] - << "> look_at <" << cameraLook[0] << "," << cameraLook[1] << "," << cameraLook[2] - << "> " ; + << "> look_at <" << cameraLook[0] << "," << cameraLook[1] << "," << cameraLook[2] << "> " ; - if (highlightAgent) { - out << "translate <" << translate[0] << "," << translate[1] << "," << translate[2] - << "> " << std::endl ; + if (highlightAgent) + { + out << "translate <" << translate[0] << "," << translate[1] << "," << translate[2] << "> " + << std::endl ; out << "rotate <" << angleX << "," << angleY << "," << angleZ << "> }" << std::endl ; } out << "}" << std::endl ; @@ -751,14 +823,14 @@ bool SocialAgents::exportScenePov(PFP::MAP& map, PFP::TVEC3& position, //set a sky sphere out << "sphere { <0, 0, 0>, 5000" ; out << "texture{ pigment { color rgb <1, 1, 1>} finish { ambient 1 diffuse 0 } } }" - << std::endl ; - -// out << "plane { <0,1,0>, -0.001"; -// out << "texture{ pigment { color rgb <0.6, 0.8, 0.6>} finish { ambient rgb 0.3 brilliance 0.2 } } }" << std::endl; - - //put some lights -// out << "light_source { <-800, 800, -800> color rgb 0.25 }"<< std::endl; + << std::endl ; +// out << "plane { <0,1,0>, -0.001" ; +// out +// << "texture{ pigment { color rgb <0.6, 0.8, 0.6>} finish { ambient rgb 0.3 brilliance 0.2 } } }" +// << std::endl ; +// out << "light_source { <-800, 800, -800> color rgb 0.25 }" << std::endl ; +// //set a high quality rendering out << "global_settings {" << std::endl ; out << "radiosity {" << std::endl ; @@ -767,70 +839,85 @@ bool SocialAgents::exportScenePov(PFP::MAP& map, PFP::TVEC3& position, // out << "count 10 nearest_count 10 error_bound 0.25 recursion_limit 1 low_error_factor 0.2 gray_threshold 0.0 minimum_reuse 0.015 brightness 1.4 adc_bailout 0.01/2 normal off media off} max_trace_level 5}" << std::endl; // else out - << "count 300 nearest_count 10 error_bound 0.10 recursion_limit 1 low_error_factor 0.2 gray_threshold 0.0 minimum_reuse 0.015 brightness 1.4 adc_bailout 0.01/2 normal off media off} max_trace_level 60}" - << std::endl ; + << "count 300 nearest_count 10 error_bound 0.10 recursion_limit 1 low_error_factor 0.2 gray_threshold 0.0 minimum_reuse 0.015 brightness 1.4 adc_bailout 0.01/2 normal off media off} max_trace_level 60}" + << std::endl ; -// if(draft) -// for(unsigned int i = 0; i< sim.agents_.size() ; ++i) { -// VEC3 pBase(sim.agents_[i]->getPosition()); -// VEC3 posR(pBase); +// if (draft) for (unsigned int i = 0; i < sim.agents_.size(); ++i) +// { +// VEC3 pBase(sim.agents_[i]->getPosition()) ; +// VEC3 posR(pBase) ; // -// Geom::Plane3D pl = Algo::Geometry::facePlane(map,sim.agents_[i]->part_.d,position); -// posR[2] -= 1000; -// Geom::intersectionPlaneRay(pl,posR,VEC3(0,0,-1),posR); +// Geom::Plane3D pl = Algo::Geometry::facePlane(map, sim.agents_[i]->part_.d, +// position) ; +// posR[2] -= 1000 ; +// Geom::intersectionPlaneRay(pl, posR, VEC3(0, 0, -1), posR) ; // -// VEC3 dir = sim.agents_[i]->velocity_; -// dir.normalize(); -// dir *= 5.0f; -// out << "cylinder {" << std::endl; -// out << "<0,0,0>, <" << dir[0] << "," << dir[1] << "," << dir[2] << ">,0.05" << std::endl; -// out << "translate <" << posR[0] << "," << posR[1] << "," << posR[2]+1+4*0.0638297872 << "> " << std::endl; -// out << "rotate <" << angleX-90 << "," << angleY << "," << angleZ << "> " << std::endl; +// VEC3 dir = sim.agents_[i]->velocity_ ; +// dir.normalize() ; +// dir *= 5.0f ; +// out << "cylinder {" << std::endl ; +// out << "<0,0,0>, <" << dir[0] << "," << dir[1] << "," << dir[2] << ">,0.05" << std::endl ; +// out << "translate <" << posR[0] << "," << posR[1] << "," << posR[2] + 1 + 4 * 0.0638297872 +// << "> " << std::endl ; +// out << "rotate <" << angleX - 90 << "," << angleY << "," << angleZ << "> " << std::endl ; // -// out << "texture{ pigment{ color rgb<0.5,0.5,1.0>} finish { ambient rgb 0.1 brilliance 0.5 } }" << std::endl; -// out << "}" << std::endl; -// } +// out +// << "texture{ pigment{ color rgb<0.5,0.5,1.0>} finish { ambient rgb 0.1 brilliance 0.5 } }" +// << std::endl ; +// out << "}" << std::endl ; +// } - for (unsigned int i = 0 ; i < sim.agents_.size() ; ++i) { + for (unsigned int i = 0; i < sim.agents_.size(); ++i) + { int typeOfAgent = 0 ; - if (draft) { -// typeOfAgent=2; -// out << "object {" << " sombrero2_lowres_ " << std::endl; -// out << "scale 0.0638297872" << std::endl; + if (draft) + { +// typeOfAgent = 2 ; +// out << "object {" << " sombrero2_lowres_ " << std::endl ; +// out << "scale 0.0638297872" << std::endl ; out << "cylinder {" << " <0,0,0>, <0,6,0>, 1.5 " << std::endl ; out << "scale 0.5" << std::endl ; VEC3 col = Utils::color_map_BCGYR(float(i) / float(sim.agents_.size())) ; out << "pigment { color <" << col[0] << "," << col[1] << "," << col[2] << "> }" - << std::endl ; - -// unsigned int col = (unsigned int)(float(i)/float(sim.agents_.size())*0xfffffe); - -// std::cout << "i" << i << " truc " << xfloat(i)/float(sim.agents_.size()) << std::endl; + << std::endl ; -// std::cout << "col " << (col/256/256)%256 << "," << (col/256)%256 << "," << col%256 << std::endl; - -// out << "pigment { color <" << (col > 16)/256.0f << "," << ((col > 8) & 0xff)/256.0f << "," << (col & 0xff)/256.0f << "> }" << std::endl; -// out << "pigment { color <" << (col/256/256)%256 << "," << (col/256)%256 << "," << col%256 << "> }" << std::endl; +// unsigned int col = (unsigned int)(float(i) / float(sim.agents_.size()) * 0xfffffe) ; +// +// std::cout << "i" << i << " truc " << xfloat(i) / float(sim.agents_.size()) +// << std::endl ; +// +// std::cout << "col " << (col / 256 / 256) % 256 << "," << (col / 256) % 256 << "," +// << col % 256 << std::endl ; +// +// out << "pigment { color <" << (col > 16) / 256.0f << "," << ((col > 8) & 0xff) / 256.0f +// << "," << (col & 0xff) / 256.0f << "> }" << std::endl ; +// out << "pigment { color <" << (col / 256 / 256) % 256 << "," << (col / 256) % 256 << "," +// << col % 256 << "> }" << std::endl ; } - else { - if (i == 0) { + else + { + if (i == 0) + { typeOfAgent = 3 ; out << "object {" << " Sheriff_animated_ " << std::endl ; out << "scale 0.165745856" << std::endl ; } - else if (i % 7 == 0) { + else if (i % 7 == 0) + { typeOfAgent = 2 ; out << "object {" << " Cowbow_animated_ " << std::endl ; out << "scale 0.170454545" << std::endl ; } - else if (i % 7 < 5) { + else if (i % 7 < 5) + { typeOfAgent = 1 ; out << "object {" << " sombrero2_highres_ " << std::endl ; out << "scale 0.127659574" << std::endl ; } - else { + else + { typeOfAgent = 0 ; out << "object {" << " sombrero3_highres_ " << std::endl ; out << "scale 0.132743363" << std::endl ; @@ -852,41 +939,42 @@ bool SocialAgents::exportScenePov(PFP::MAP& map, PFP::TVEC3& position, int sign = axisRot[2] > 0 ? 1 : -1 ; // // //57,2957795 : conversion from radian to degree - float myRot = acos( -dir[1]) * 57.2957795f ; + float myRot = acos(-dir[1]) * 57.2957795f ; // // out << "rotate <0,0," << -90 + sign * myRot << ">" << std::endl ; - out << "rotate <" << angleX - 90 << "," << angleY << "," << angleZ << "> " - << std::endl ; + out << "rotate <" << angleX - 90 << "," << angleY << "," << angleZ << "> " << std::endl ; // out << "translate <" << posR[0] << "," << posR[1] << "," << posR[2]+1+typeOfAgent*4*0.0638297872 << "> " << std::endl; - out << "translate <" << posR[0] << "," << posR[2] << "," << posR[1] << "> " - << std::endl ; + out << "translate <" << posR[0] << "," << posR[2] << "," << posR[1] << "> " << std::endl ; - out << "translate <" << translate[0] << "," << translate[1] << "," << translate[2] - << ">" << std::endl ; + out << "translate <" << translate[0] << "," << translate[1] << "," << translate[2] << ">" + << std::endl ; out << "}" << std::endl ; - if (exportPath) { + if (exportPath) + { out << "sphere_sweep { cubic_spline" << std::endl ; out << sim.agents_[i]->goals_.size() << "," << std::endl ; - for (unsigned int j = 0 ; j < sim.agents_[i]->goals_.size() ; ++j) { - out << "<" << sim.agents_[i]->goals_[j][0] << "," - << sim.agents_[i]->goals_[j][2] << "," << sim.agents_[i]->goals_[j][1] - << ">, 4" << std::endl ; + for (unsigned int j = 0; j < sim.agents_[i]->goals_.size(); ++j) + { + out << "<" << sim.agents_[i]->goals_[j][0] << "," << sim.agents_[i]->goals_[j][2] + << "," << sim.agents_[i]->goals_[j][1] << ">, 4" << std::endl ; } VEC3 col = Utils::color_map_BCGYR(float(i) / float(sim.agents_.size())) ; out << "pigment { color <" << col[0] << "," << col[1] << "," << col[2] << "> }" - << std::endl ; + << std::endl ; out << " }" << std::endl ; } } - if (infoFaces) { + if (infoFaces) + { CellMarker m(sim.envMap_.map, FACE) ; - for (Dart d = sim.envMap_.map.begin() ; d != sim.envMap_.map.end() ; - sim.envMap_.map.next(d)) { - if ( !m.isMarked(d)) { + for (Dart d = sim.envMap_.map.begin(); d != sim.envMap_.map.end(); sim.envMap_.map.next(d)) + { + if (!m.isMarked(d)) + { m.mark(d) ; exportInfoFace(out, d) ; } @@ -895,15 +983,19 @@ bool SocialAgents::exportScenePov(PFP::MAP& map, PFP::TVEC3& position, // if(!draft) { - if (highlightAgent) { + if (highlightAgent) + { //find the cells to highlight DartMarkerStore highlight(map) ; Agent * ag = sim.agents_[highlightAgentNo] ; - for (Dart d = map.begin() ; d != map.end() ; map.next(d)) { - if ( !highlight.isMarked(d)) { + for (Dart d = map.begin(); d != map.end(); map.next(d)) + { + if (!highlight.isMarked(d)) + { PFP::AGENTS listAgentInCell = sim.envMap_.agentvect[d] ; if (std::find(listAgentInCell.begin(), listAgentInCell.end(), ag) - != listAgentInCell.end()) { + != listAgentInCell.end()) + { highlight.markOrbit(FACE, d) ; } } @@ -917,14 +1009,15 @@ bool SocialAgents::exportScenePov(PFP::MAP& map, PFP::TVEC3& position, out << "object {facesHighlighted" << std::endl ; out << "rotate <" << angleX << "," << angleY << "," << angleZ << "> " << std::endl ; out << "translate <" << translate[0] << "," << translate[1] << "," << translate[2] - << ">" << std::endl ; + << ">" << std::endl ; out - << "texture{ pigment{ color rgb<0.4667,0.709,0.996>} finish { ambient rgb 0.35 brilliance 0.5 } } }" - << std::endl ; + << "texture{ pigment{ color rgb<0.4667,0.709,0.996>} finish { ambient rgb 0.35 brilliance 0.5 } } }" + << std::endl ; } - else { + else + { Algo::ExportPov::exportMeshPlain(out, sim.envMap_.map, sim.envMap_.position, - "myMesh", good) ; + "myMesh", good) ; // Algo::ExportPov::exportMeshPlain(out,sim.envMap_.mapScenary,sim.envMap_.positionScenary,"myMesh",good); // @@ -955,22 +1048,22 @@ bool SocialAgents::exportScenePov(PFP::MAP& map, PFP::TVEC3& position, // Algo::ExportPov::exportMeshPlain(out,map,position,"myCity",roadM); } - if (wireDisplay) { - Algo::ExportPov::exportMeshWire(out, map, position, "myMeshWire", - SelectorTrue()) ; + if (wireDisplay) + { + Algo::ExportPov::exportMeshWire(out, map, position, "myMeshWire", SelectorTrue()) ; out << "object {myMeshWire" << std::endl ; out << "rotate <" << angleX << "," << angleY << "," << angleZ << "> " << std::endl ; out << "translate <" << translate[0] << "," << translate[1] << "," << translate[2] - << ">" << std::endl ; + << ">" << std::endl ; out << "texture{ pigment{ color rgb<0,0,0>} } }" << std::endl ; } out << "object {myMesh" << std::endl ; out << "rotate <" << angleX << "," << angleY << "," << angleZ << "> " << std::endl ; - out << "translate <" << translate[0] << "," << translate[1] << "," << translate[2] - << ">" << std::endl ; + out << "translate <" << translate[0] << "," << translate[1] << "," << translate[2] << ">" + << std::endl ; out << "texture{ pigment{ color rgb 0.7} finish { ambient rgb 0.3 brilliance 0.5 } } }" - << std::endl ; + << std::endl ; // out << "object {myCityPed" << std::endl; // out << "rotate <" << angleX << "," << angleY << "," << angleZ << "> " << std::endl; @@ -993,70 +1086,80 @@ bool SocialAgents::exportScenePov(PFP::MAP& map, PFP::TVEC3& position, void SocialAgents::cb_keyPress(int keycode) { - switch (keycode) { - case 'a' : { - if (timer->isActive()) timer->stop() ; - else timer->start() ; - dock.check_timer->setChecked(timer->isActive()) ; - break ; - } - case 'c' : { - sim.envMap_.map.check() ; - break ; - } - case 'e' : { - std::cout << "exporting obstacle to file myScene.obst" << std::endl ; - std::string filename("myScene.obst") ; - CGoGN::ExportScene::exportSceneToFile(sim.envMap_.map, sim.envMap_.position, - sim.envMap_.obstacleMark, sim.envMap_.buildingMark, filename) ; - std::string filename2("myAgents.pos") ; - sim.exportAgents(filename2) ; - break ; - } - case 'f' : { - displayFps = !displayFps ; - break ; - } - case 'p' : { - std::cout << sim.globalTime_ << std::endl ; - break ; - } + switch (keycode) + { + case 'a' : + { + if (timer->isActive()) + timer->stop() ; + else + timer->start() ; + dock.check_timer->setChecked(timer->isActive()) ; + break ; + } + case 'c' : + { + sim.envMap_.map.check() ; + break ; + } + case 'e' : + { + std::cout << "exporting obstacle to file myScene.obst" << std::endl ; + std::string filename("myScene.obst") ; + CGoGN::ExportScene::exportSceneToFile(sim.envMap_.map, sim.envMap_.position, + sim.envMap_.obstacleMark, + sim.envMap_.buildingMark, filename) ; + std::string filename2("myAgents.pos") ; + sim.exportAgents(filename2) ; + break ; + } + case 'p' : + { + std::cout << sim.globalTime_ << std::endl ; + break ; + } #ifndef SPATIAL_HASHING - case 'r' : { - exportScenePov(sim.envMap_.map, sim.envMap_.position, - "planSerre/exportSceneOle0000.pov", VEC3(10, 3, 10), VEC3(0, 0, 0), - VEC3(1.0f, 0, 0), 0, 0, 0) ; -// exportScenePov(sim.envMap_.map,sim.envMap_.position,"exportScene0002.pov",VEC3(43,762,1565),VEC3(0,762,0),VEC3(1.0f,0,0),0,0,0); -// exportScenePov(sim.envMap_.map,sim.envMap_.position,"exportScene0002.pov",VEC3(43,762,65),VEC3(0,762,0),VEC3(1.0f,0,0),0,0,0); - break ; - } + case 'r' : + { + exportScenePov(sim.envMap_.map, sim.envMap_.position, "planSerre/exportSceneOle0000.pov", + VEC3(10, 3, 10), VEC3(0, 0, 0), VEC3(1.0f, 0, 0), 0, 0, 0) ; +// exportScenePov(sim.envMap_.map, sim.envMap_.position, "exportScene0002.pov", +// VEC3(43, 762, 1565), VEC3(0, 762, 0), VEC3(1.0f, 0, 0), 0, 0, 0) ; +// exportScenePov(sim.envMap_.map, sim.envMap_.position, "exportScene0002.pov", +// VEC3(43, 762, 65), VEC3(0, 762, 0), VEC3(1.0f, 0, 0), 0, 0, 0) ; + break ; + } #endif - case 's' : { - animate() ; - break ; - } - case '5' : { - FILE *fp ; - int state = GL2PS_OVERFLOW , buffsize = 0 ; - - fp = fopen("out.svg", "wb") ; - printf("Writing 'out.eps'... ") ; - while (state == GL2PS_OVERFLOW) { - buffsize += 1024 * 1024 ; - gl2psBeginPage("test", "gl2psTestSimple", NULL, GL2PS_SVG, GL2PS_SIMPLE_SORT, - GL2PS_USE_CURRENT_VIEWPORT, GL_RGBA, 0, NULL, 0, 0, 0, buffsize, fp, - "out.svg") ; - updateGL() ; - state = gl2psEndPage() ; - } - fclose(fp) ; - printf("Done!\n") ; - break ; - } - case '9' : { - render_anim = !render_anim ; - break ; + case 's' : + { + animate() ; + break ; + } + case '5' : + { + FILE *fp ; + int state = GL2PS_OVERFLOW, buffsize = 0 ; + + fp = fopen("out.svg", "wb") ; + printf("Writing 'out.eps'... ") ; + while (state == GL2PS_OVERFLOW) + { + buffsize += 1024 * 1024 ; + gl2psBeginPage("test", "gl2psTestSimple", NULL, GL2PS_SVG, GL2PS_SIMPLE_SORT, + GL2PS_USE_CURRENT_VIEWPORT, GL_RGBA, 0, NULL, 0, 0, 0, buffsize, fp, + "out.svg") ; + updateGL() ; + state = gl2psEndPage() ; } + fclose(fp) ; + printf("Done!\n") ; + break ; + } + case '9' : + { + render_anim = !render_anim ; + break ; + } } updateGL() ; @@ -1070,20 +1173,19 @@ int main(int argc, char** argv) { QApplication app(argc, argv) ; - SocialAgents sa ; - sa.setGeometry(0, 0, 1200, 800) ; + int minSize = Agent::neighborDist_ ; + int iterations = 0; + + if (argc > 2) iterations = atoi(argv[2]) ; + if (argc > 1) { + minSize = atoi(argv[1]) ; + if (iterations == 0) iterations = 4000 ; + } + SocialAgents sa(minSize,iterations) ; + sa.setGeometry(0, 0, 1800, 1200) ; sa.initGUI() ; sa.show() ; - -// if(argc == 2) -// { -// GLint t1 = glutGet(GLUT_ELAPSED_TIME); -// for(unsigned int i = 0; i < 300; ++i) -// sa->animate(); -// GLint t2 = glutGet(GLUT_ELAPSED_TIME); -// GLfloat seconds = (t2 - t1) / 1000.0f; -// std::cout << "animation : "<< seconds << "sec" << std::endl; -// } + if (argc > 1) sa.timer->start() ; return app.exec() ; }