├── CMakeLists.txt ├── COPYING ├── ChangeLog ├── Doxyfile ├── README ├── cmake ├── FindGLES.cmake ├── FindGLES2.cmake ├── FindGLEW.cmake └── FindSDL_gles.cmake ├── doc └── architecture.dia ├── examples ├── CMakeLists.txt └── SimpleViewer.cc ├── libglosm-client ├── CMakeLists.txt ├── CheckGL.cc ├── FirstPersonViewer.cc ├── GPXLayer.cc ├── GPXTile.cc ├── GeometryLayer.cc ├── GeometryTile.cc ├── MercatorProjection.cc ├── OrthoViewer.cc ├── Projection.cc ├── SphericalProjection.cc ├── TerrainLayer.cc ├── TerrainTile.cc ├── TileManager.cc ├── glosm │ ├── CheckGL.hh │ ├── FirstPersonViewer.hh │ ├── GPXLayer.hh │ ├── GPXTile.hh │ ├── GeometryLayer.hh │ ├── GeometryTile.hh │ ├── Layer.hh │ ├── MercatorProjection.hh │ ├── OrthoViewer.hh │ ├── Projection.hh │ ├── Renderable.hh │ ├── SphericalProjection.hh │ ├── TerrainLayer.hh │ ├── TerrainTile.hh │ ├── Tile.hh │ ├── TileManager.hh │ ├── VertexBuffer.hh │ ├── Viewer.hh │ └── util │ │ └── gl.h ├── mglu.cc └── mglu.h ├── libglosm-geomgen ├── CMakeLists.txt ├── GeometryGenerator.cc ├── MetricBasis.cc └── glosm │ ├── GeometryGenerator.hh │ └── MetricBasis.hh ├── libglosm-server ├── BBox.cc ├── CMakeLists.txt ├── DummyHeightmap.cc ├── Exception.cc ├── Geometry.cc ├── GeometryOperations.cc ├── Guard.cc ├── ParsingHelpers.cc ├── PreloadedGPXDatasource.cc ├── PreloadedXmlDatasource.cc ├── SRTMDatasource.cc ├── Timer.cc ├── WayMerger.cc ├── XMLParser.cc └── glosm │ ├── BBox.hh │ ├── DummyHeightmap.hh │ ├── Exception.hh │ ├── GPXDatasource.hh │ ├── Geometry.hh │ ├── GeometryDatasource.hh │ ├── GeometryOperations.hh │ ├── Guard.hh │ ├── HeightmapDatasource.hh │ ├── Math.hh │ ├── Misc.hh │ ├── NonCopyable.hh │ ├── OsmDatasource.hh │ ├── ParsingHelpers.hh │ ├── PreloadedGPXDatasource.hh │ ├── PreloadedXmlDatasource.hh │ ├── SRTMDatasource.hh │ ├── Timer.hh │ ├── WayMerger.hh │ ├── XMLParser.hh │ ├── geomath.h │ ├── id_map.hh │ └── osmtypes.h ├── testdata ├── glosm.gpx ├── glosm.osm ├── grid.osm └── halfgrid.osm ├── tests ├── CMakeLists.txt ├── ExceptionTest.cc ├── IdMapTest.cc ├── ProjectionBench.cc ├── ProjectionTest.cc ├── TypeTest.cc └── testing.h ├── tiler ├── CMakeLists.txt ├── Main.cc ├── PBuffer.cc ├── PBuffer.hh ├── PixelBuffer.cc ├── PixelBuffer.hh ├── PngWriter.cc └── PngWriter.hh └── viewer ├── CMakeLists.txt ├── GLUTMain.cc ├── GlosmViewer.cc ├── GlosmViewer.hh └── SDLMain.cc /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | PROJECT(glosm) 2 | 3 | CMAKE_MINIMUM_REQUIRED(VERSION 2.6) 4 | 5 | SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) 6 | 7 | # Global options 8 | IF(UNIX) 9 | SET(BUILD_TILER_DEFAULT ON) 10 | ELSE(UNIX) 11 | SET(BUILD_TILER_DEFAULT OFF) 12 | ENDIF(UNIX) 13 | 14 | IF(WIN32) 15 | SET(WITH_GLEW_DEFAULT ON) 16 | ELSE(WIN32) 17 | SET(WITH_GLEW_DEFAULT OFF) 18 | ENDIF(WIN32) 19 | 20 | OPTION(BUILD_VIEWER_SDL "Build first-person osm viewer (SDL version)" ON) 21 | OPTION(BUILD_VIEWER_GLUT "Build first-person osm viewer (GLUT version)" OFF) 22 | OPTION(BUILD_TILER "Build tile generator" ${BUILD_TILER_DEFAULT}) 23 | OPTION(BUILD_EXAMPLES "Build examples" OFF) 24 | OPTION(BUILD_TESTS "Build tests" ON) 25 | OPTION(WITH_GLEW "Use GLEW (needed when you system uses archaic OpenGL)" ${WITH_GLEW_DEFAULT}) 26 | OPTION(WITH_GLES "Use OpenGL ES 1.1" OFF) 27 | # TODO: this requires rewriting some legacy OpenGL code 28 | #OPTION(WITH_GLES2 "Use OpenGL ES 2.0" OFF) 29 | OPTION(WITH_TOUCHPAD "Tune control for touchpad instead of mouse" OFF) 30 | OPTION(DEBUG_TILING "Render tile bounds for debugging" OFF) 31 | OPTION(DEBUG_FPS "Don't limit FPS for profiling" OFF) 32 | 33 | # Global variables (still overridable via cmake -D ..) 34 | SET(BINDIR "bin" CACHE STRING "Install subdirectory for binaries") 35 | SET(LIBDIR "lib" CACHE STRING "Install subdirectory for libraries") 36 | 37 | # Conditionals 38 | IF(MINGW) 39 | # Leave console till we have better way to display "Loading..." message 40 | SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -mconsole") 41 | ENDIF(MINGW) 42 | 43 | # TODO: these should be disabled if only server software is built 44 | IF(WITH_GLES) 45 | FIND_PACKAGE(GLES REQUIRED) 46 | ADD_DEFINITIONS(-DWITH_GLES) 47 | SET(GL_INCLUDE_DIRS ${GLES_INCLUDE_DIR}) 48 | SET(GL_LIBRARIES ${GLES_LIBRARY}) 49 | ELSEIF(WITH_GLES2) 50 | FIND_PACKAGE(GLES2 REQUIRED) 51 | ADD_DEFINITIONS(-DWITH_GLES2) 52 | SET(GL_INCLUDE_DIRS ${GLES2_INCLUDE_DIR}) 53 | SET(GL_LIBRARIES ${GLES2_LIBRARY}) 54 | ELSE(WITH_GLES) 55 | FIND_PACKAGE(OpenGL REQUIRED) 56 | SET(GL_INCLUDE_DIRS ${OPENGL_INCLUDE_DIR}) 57 | SET(GL_LIBRARIES ${OPENGL_gl_LIBRARY}) 58 | ENDIF(WITH_GLES) 59 | 60 | IF(WITH_GLEW) 61 | FIND_PACKAGE(GLEW REQUIRED) 62 | ADD_DEFINITIONS(-DWITH_GLEW) 63 | SET(GL_INCLUDE_DIRS ${GL_INCLUDE_DIRS} ${GLEW_INCLUDE_DIR}) 64 | SET(GL_LIBRARIES ${GL_LIBRARIES} ${GLEW_LIBRARY}) 65 | ENDIF(WITH_GLEW) 66 | 67 | # Global definitions 68 | ADD_DEFINITIONS(-Wall -Wextra -Wno-unused-parameter) 69 | IF(DEBUG_TILING) 70 | ADD_DEFINITIONS(-DDEBUG_TILING) 71 | ENDIF(DEBUG_TILING) 72 | IF(DEBUG_FPS) 73 | ADD_DEFINITIONS(-DDEBUG_FPS) 74 | ENDIF(DEBUG_FPS) 75 | 76 | # Info 77 | MESSAGE(STATUS " Building SDL viewer: ${BUILD_VIEWER_SDL}") 78 | MESSAGE(STATUS " Building GLUT viewer: ${BUILD_VIEWER_GLUT}") 79 | MESSAGE(STATUS " Building tiler: ${BUILD_TILER}") 80 | MESSAGE(STATUS " Building examples: ${BUILD_EXAMPLES}") 81 | MESSAGE(STATUS " Building tests: ${BUILD_TESTS}") 82 | MESSAGE(STATUS "") 83 | MESSAGE(STATUS " GLEW support: ${WITH_GLEW}") 84 | MESSAGE(STATUS "OpenGL ES 1.1 support: ${WITH_GLES}") 85 | #MESSAGE(STATUS "OpenGL ES 2.0 support: ${WITH_GLES2}") 86 | MESSAGE(STATUS " Touchpad support: ${WITH_TOUCHPAD}") 87 | 88 | # Framework subdirs 89 | ADD_SUBDIRECTORY(libglosm-server) 90 | ADD_SUBDIRECTORY(libglosm-client) 91 | ADD_SUBDIRECTORY(libglosm-geomgen) 92 | 93 | # Application subdirs 94 | IF(BUILD_VIEWER_SDL OR BUILD_VIEWER_GLUT) 95 | ADD_SUBDIRECTORY(viewer) 96 | ENDIF(BUILD_VIEWER_SDL OR BUILD_VIEWER_GLUT) 97 | IF(BUILD_TILER) 98 | ADD_SUBDIRECTORY(tiler) 99 | ENDIF(BUILD_TILER) 100 | IF(BUILD_EXAMPLES) 101 | ADD_SUBDIRECTORY(examples) 102 | ENDIF(BUILD_EXAMPLES) 103 | IF(BUILD_TESTS) 104 | ENABLE_TESTING() 105 | ADD_SUBDIRECTORY(tests) 106 | ENDIF(BUILD_TESTS) 107 | -------------------------------------------------------------------------------- /ChangeLog: -------------------------------------------------------------------------------- 1 | glosm-0.0.2 2011-02-27 2 | -------------------------------- 3 | * Geometry tiling was implemented: there are now two layers of 4 | geometry, ground (lowres) and detail (3D objects) which are split 5 | into tiles which are loaded on demand in background thread 6 | * Viewer may now be built with either of SDL and GLUT. SDL version 7 | is recommended and is enabled by default 8 | * Library was split into three: libglosm-server (core features), 9 | libglosm-client (OpenGL stuff), libglosm-geomgen (geometry 10 | generator) 11 | * Navigation was improved (more speed control, height locking, 12 | disabling mouse grab, touchpad-friendly navigation mode) 13 | * Added Windows support through cross-compilation with mingw32 14 | * Added OpenGL ES 1.1 platforms support (namely Maemo) 15 | * Added Spherical projection (in which Earth looks like globe) in 16 | addition to Mercator (flat Earth) 17 | * Added support for man_made=tower and man_made=chimney area objects 18 | * Added support for building:roof:shape=conical as the alias for 19 | pyramidal, both are now supported for arbitrary shaped buildings 20 | * Fixed crash in geometry generator 21 | 22 | glosm-0.0.1 2011-01-18 23 | -------------------------------- 24 | * First public release 25 | -------------------------------------------------------------------------------- /Doxyfile: -------------------------------------------------------------------------------- 1 | # Doxyfile for glosm; please keep tags sourted and grouped as in 2 | # vanilla Doxyfile generated with `doxygen -g' 3 | 4 | # Project related configuration options 5 | PROJECT = glosm 6 | OUTPUT_DIRECTORY = doc 7 | OUTPUT_LANGUAGE = English 8 | JAVADOC_AUTOBRIEF = yes 9 | TAB_SIZE = 4 10 | 11 | # Build related configuration options 12 | 13 | # configuration options related to warning and progress messages 14 | 15 | # configuration options related to the input files 16 | INPUT = libglosm-server libglosm-client libgeomgen-default 17 | RECURSIVE = yes 18 | EXCLUDE = libglosm-server/glosm/id_map.hh 19 | 20 | # configuration options related to source browsing 21 | 22 | # configuration options related to the alphabetical class index 23 | 24 | # configuration options related to the HTML output 25 | 26 | # configuration options related to the LaTeX output 27 | GENERATE_LATEX = NO 28 | 29 | # configuration options related to the RTF output 30 | 31 | # configuration options related to the man page output 32 | 33 | # configuration options related to the XML output 34 | 35 | # configuration options for the AutoGen Definitions output 36 | 37 | # configuration options related to the Perl module output 38 | 39 | # Configuration options related to the preprocessor 40 | INCLUDE_PATH = libglosm-server/include libglosm-client/include libgeomgen-default/include 41 | 42 | # Configuration::additions related to external references 43 | 44 | # Configuration options related to the dot tool 45 | HAVE_DOT = yes 46 | CALL_GRAPH = yes 47 | CALLER_GRAPH = yes 48 | -------------------------------------------------------------------------------- /cmake/FindGLES.cmake: -------------------------------------------------------------------------------- 1 | # Find GLES 2 | # 3 | # GLES_INCLUDE_DIR 4 | # GLES_LIBRARY 5 | # GLES_FOUND 6 | 7 | FIND_PATH(GLES_INCLUDE_DIR NAMES GLES/gl.h) 8 | 9 | FIND_LIBRARY(GLES_LIBRARY NAMES GLES_CM) 10 | 11 | INCLUDE(FindPackageHandleStandardArgs) 12 | FIND_PACKAGE_HANDLE_STANDARD_ARGS(GLES DEFAULT_MSG GLES_LIBRARY GLES_INCLUDE_DIR) 13 | 14 | MARK_AS_ADVANCED(GLES_INCLUDE_DIR GLES_LIBRARY) 15 | -------------------------------------------------------------------------------- /cmake/FindGLES2.cmake: -------------------------------------------------------------------------------- 1 | # Find GLES2 2 | # 3 | # GLES2_INCLUDE_DIR 4 | # GLES2_LIBRARY 5 | # GLES2_FOUND 6 | 7 | FIND_PATH(GLES2_INCLUDE_DIR NAMES GLES2/gl2.h) 8 | 9 | FIND_LIBRARY(GLES2_LIBRARY NAMES GLESv2) 10 | 11 | INCLUDE(FindPackageHandleStandardArgs) 12 | FIND_PACKAGE_HANDLE_STANDARD_ARGS(GLES2 DEFAULT_MSG GLES2_LIBRARY GLES2_INCLUDE_DIR) 13 | 14 | MARK_AS_ADVANCED(GLES2_INCLUDE_DIR GLES2_LIBRARY) 15 | -------------------------------------------------------------------------------- /cmake/FindGLEW.cmake: -------------------------------------------------------------------------------- 1 | # Find GLEW 2 | # 3 | # GLEW_INCLUDE_DIR 4 | # GLEW_LIBRARY 5 | # GLEW_FOUND 6 | # 7 | 8 | FIND_PATH(GLEW_INCLUDE_DIR NAMES GL/glew.h) 9 | 10 | FIND_LIBRARY(GLEW_LIBRARY NAMES GLEW glew glew32 GLEW) 11 | 12 | INCLUDE(FindPackageHandleStandardArgs) 13 | FIND_PACKAGE_HANDLE_STANDARD_ARGS(GLEW DEFAULT_MSG GLEW_LIBRARY GLEW_INCLUDE_DIR) 14 | 15 | MARK_AS_ADVANCED(GLEW_INCLUDE_DIR GLEW_LIBRARY) 16 | -------------------------------------------------------------------------------- /cmake/FindSDL_gles.cmake: -------------------------------------------------------------------------------- 1 | # Find SDL_gles 2 | # 3 | # SDLGLES_INCLUDE_DIR 4 | # SDLGLES_LIBRARY 5 | # SDLGLES_FOUND 6 | 7 | FIND_PATH(SDLGLES_INCLUDE_DIR NAMES SDL/SDL_gles.h) 8 | 9 | FIND_LIBRARY(SDLGLES_LIBRARY NAMES SDL_gles) 10 | 11 | INCLUDE(FindPackageHandleStandardArgs) 12 | FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDLGLES DEFAULT_MSG SDLGLES_LIBRARY SDLGLES_INCLUDE_DIR) 13 | 14 | MARK_AS_ADVANCED(SDLGLES_INCLUDE_DIR SDLGLES_LIBRARY) 15 | -------------------------------------------------------------------------------- /doc/architecture.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AMDmi3/glosm/98e39883ac437861dc62d147e4fb24bfe145ab5e/doc/architecture.dia -------------------------------------------------------------------------------- /examples/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Depends 2 | FIND_PACKAGE(GLUT REQUIRED) 3 | 4 | IF(WITH_GLEW) 5 | FIND_PACKAGE(GLEW REQUIRED) 6 | ADD_DEFINITIONS(-DWITH_GLEW) 7 | ENDIF(WITH_GLEW) 8 | 9 | # Targets 10 | INCLUDE_DIRECTORIES(${OPENGL_INCLUDE_DIR} ${GLUT_INCLUDE_DIR} ${GLEW_INCLUDE_DIR} ../libglosm-client ../libglosm-server ../libglosm-geomgen) 11 | ADD_DEFINITIONS(-DTESTDATA=\"${PROJECT_SOURCE_DIR}/testdata/glosm.osm\") 12 | 13 | ADD_EXECUTABLE(SimpleViewer SimpleViewer.cc) 14 | TARGET_LINK_LIBRARIES(SimpleViewer glosm-client glosm-server glosm-geomgen ${GLUT_LIBRARY} ${GLEW_LIBRARY}) 15 | -------------------------------------------------------------------------------- /examples/SimpleViewer.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | /* 22 | * This is nearly simplest possible glosm example. 23 | * 24 | * This application is basically glosm-viewer stripped of most 25 | * functionality to show you how glosm works. 26 | */ 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | 37 | #if defined(WITH_GLEW) 38 | # include 39 | #endif 40 | #include 41 | #include 42 | #include 43 | 44 | Viewer* g_viewer = NULL; 45 | GeometryLayer* g_layer = NULL; 46 | 47 | void Display(void) { 48 | glClearColor(0.5, 0.5, 0.5, 0.0); 49 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 50 | 51 | /*** glosm stuff begins ***/ 52 | 53 | if (g_layer && g_viewer) 54 | g_layer->Render(*g_viewer); 55 | 56 | /*** glosm stuff ends ***/ 57 | 58 | glFlush(); 59 | } 60 | 61 | void KeyDown(unsigned char, int, int) { 62 | exit(0); 63 | } 64 | 65 | int main(int argc, char** argv) { 66 | /* GLUT init */ 67 | glutInit(&argc, argv); 68 | glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA); 69 | glutInitWindowSize(800, 600); 70 | glutCreateWindow("SimpleViewer"); 71 | glutDisplayFunc(Display); 72 | glutKeyboardFunc(KeyDown); 73 | 74 | #if defined WITH_GLEW 75 | glewInit(); 76 | #endif 77 | 78 | /*** glosm stuff begins ***/ 79 | 80 | /* 1) Load our data */ 81 | PreloadedXmlDatasource osm_datasource; 82 | osm_datasource.Load(TESTDATA); 83 | 84 | /* 2) Create heightmap datasource */ 85 | DummyHeightmap heightmap; 86 | 87 | /* 3) Create facility to translate OSM data to geometry */ 88 | GeometryGenerator geometry_generator(osm_datasource, heightmap); 89 | 90 | /* 4) Create layer which will render that geometry */ 91 | GeometryLayer layer(MercatorProjection(), geometry_generator); 92 | 93 | /* 5) Request all data to be loaded synchronously */ 94 | layer.LoadArea(geometry_generator.GetBBox(), TileManager::SYNC); 95 | 96 | /* 6) Create viewer to control eye position and direction */ 97 | FirstPersonViewer viewer; 98 | viewer.SetPos(Vector3i(geometry_generator.GetCenter(), 100 * GEOM_UNITSINMETER /* 100 meters */)); 99 | viewer.SetAspect(800.0/600.0); 100 | viewer.SetRotation(-135.0 / 180.0 * M_PI, -60.0 / 180.0 * M_PI); 101 | 102 | /*** glosm stuff ends ***/ 103 | 104 | g_viewer = &viewer; 105 | g_layer = &layer; 106 | 107 | /* main loop */ 108 | glutMainLoop(); 109 | 110 | return 0; 111 | } 112 | -------------------------------------------------------------------------------- /libglosm-client/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Depends 2 | FIND_PACKAGE(Threads REQUIRED) 3 | 4 | # Checks 5 | IF(NOT CMAKE_CROSSCOMPILING) 6 | INCLUDE(CheckFunctionExists) 7 | SET(CMAKE_EXTRA_INCLUDE_FILES "math.h") 8 | SET(CMAKE_REQUIRED_LIBRARIES m) 9 | CHECK_FUNCTION_EXISTS(sincos HAVE_SINCOS) 10 | SET(CMAKE_REQUIRED_LIBRARIES) 11 | SET(CMAKE_EXTRA_INCLUDE_FILES) 12 | ENDIF(NOT CMAKE_CROSSCOMPILING) 13 | 14 | IF(HAVE_SINCOS) 15 | ADD_DEFINITIONS(-DHAVE_SINCOS) 16 | ENDIF(HAVE_SINCOS) 17 | 18 | # Targets 19 | SET(SOURCES 20 | CheckGL.cc 21 | FirstPersonViewer.cc 22 | GeometryLayer.cc 23 | GeometryTile.cc 24 | GPXLayer.cc 25 | GPXTile.cc 26 | MercatorProjection.cc 27 | mglu.cc 28 | OrthoViewer.cc 29 | Projection.cc 30 | SphericalProjection.cc 31 | TerrainLayer.cc 32 | TerrainTile.cc 33 | TileManager.cc 34 | ) 35 | 36 | # We check whether GL functions != NULL there. These funcions may 37 | # really be either function pointers (which are perfectly OK to 38 | # compare with NULL) or normal functions (for which warning will 39 | # be generated, while comparison still gives correct result). 40 | # We have to ignore the warning because there can be mix of both 41 | SET_SOURCE_FILES_PROPERTIES( 42 | CheckGL.cc 43 | PROPERTIES 44 | COMPILE_FLAGS "-Wno-address" 45 | ) 46 | 47 | SET(HEADERS 48 | glosm/CheckGL.hh 49 | glosm/FirstPersonViewer.hh 50 | glosm/GeometryLayer.hh 51 | glosm/GeometryTile.hh 52 | glosm/GPXLayer.hh 53 | glosm/GPXTile.hh 54 | glosm/Layer.hh 55 | glosm/MercatorProjection.hh 56 | glosm/OrthoViewer.hh 57 | glosm/Projection.hh 58 | glosm/Renderable.hh 59 | glosm/SphericalProjection.hh 60 | glosm/TerrainLayer.hh 61 | glosm/TerrainTile.hh 62 | glosm/Tile.hh 63 | glosm/TileManager.hh 64 | glosm/VertexBuffer.hh 65 | glosm/Viewer.hh 66 | ) 67 | 68 | INCLUDE_DIRECTORIES(. ../libglosm-server ${EXPAT_INCLUDE_DIR} ${GL_INCLUDE_DIRS}) 69 | 70 | ADD_LIBRARY(glosm-client SHARED ${SOURCES}) 71 | TARGET_LINK_LIBRARIES(glosm-client glosm-server ${GL_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) 72 | 73 | # Installation 74 | # SET_TARGET_PROPERTIES(glosm-client PROPERTIES SOVERSION 1) 75 | INSTALL(FILES ${HEADERS} DESTINATION include/glosm) 76 | INSTALL(FILES glosm/util/gl.h DESTINATION include/glosm/util) 77 | 78 | IF(NOT CMAKE_CROSSCOMPILING) 79 | INSTALL(TARGETS glosm-client LIBRARY DESTINATION ${LIBDIR}) 80 | ENDIF(NOT CMAKE_CROSSCOMPILING) 81 | -------------------------------------------------------------------------------- /libglosm-client/CheckGL.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #include 22 | 23 | #include 24 | 25 | #define CHECK_GL_FUNCTION(x) \ 26 | if (x == NULL) \ 27 | throw GLUnsupportedException() << "Required OpenGL function " << #x << " is not supported on this platform"; \ 28 | 29 | void CheckGL() { 30 | CHECK_GL_FUNCTION(glBindBuffer); 31 | CHECK_GL_FUNCTION(glBufferData); 32 | CHECK_GL_FUNCTION(glDeleteBuffers); 33 | CHECK_GL_FUNCTION(glDrawArrays); 34 | CHECK_GL_FUNCTION(glGenBuffers); 35 | } 36 | -------------------------------------------------------------------------------- /libglosm-client/FirstPersonViewer.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #include 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #include "mglu.h" 29 | 30 | #include "glosm/util/gl.h" 31 | 32 | #include 33 | 34 | FirstPersonViewer::FirstPersonViewer(): heightmap_(NULL), pos_(), landscape_height_(0), yaw_(0), pitch_(0), fov_(90.0), aspect_(1.0) { 35 | } 36 | 37 | FirstPersonViewer::FirstPersonViewer(const Vector3i& pos): heightmap_(NULL), pos_(pos), landscape_height_(0), yaw_(0), pitch_(0), fov_(90.0), aspect_(1.0) { 38 | } 39 | 40 | FirstPersonViewer::FirstPersonViewer(const Vector3i& pos, float yaw, float pitch): heightmap_(NULL), pos_(pos), landscape_height_(0), yaw_(yaw), pitch_(pitch), fov_(90.0), aspect_(1.0) { 41 | } 42 | 43 | void FirstPersonViewer::SetupViewerMatrix(const Projection& projection) const { 44 | glMatrixMode(GL_PROJECTION); 45 | glLoadIdentity(); 46 | 47 | /* length of a meter in local units */ 48 | float meterlen = projection.Project(pos_.Flattened() + Vector3i(0, 0, GEOM_UNITSINMETER), pos_.Flattened()).z; 49 | 50 | /* viewer height in meters */ 51 | float height = pos_.z / (float)GEOM_UNITSINMETER; 52 | if (height < 100.0f) 53 | height = 100.0f; 54 | 55 | /* viewing distances is [1meter..100km] at under 100m height 56 | * and increases linearly with going higher */ 57 | float znear = 0.01f * height * meterlen; 58 | float zfar = 1000.0f * height * meterlen; 59 | 60 | mgluPerspective(fov_ / M_PI * 180.0f, aspect_, znear, zfar); 61 | 62 | glMatrixMode(GL_MODELVIEW); 63 | glLoadIdentity(); 64 | 65 | Vector3f dir = GetDirection(); 66 | Vector3f up = Vector3f(0.0f, 0.0f, 1.0f); 67 | 68 | mgluLookAt(0.0f, 0.0f, 0.0f, dir.x, dir.y, dir.z, up.x, up.y, up.z); 69 | } 70 | 71 | Vector3i FirstPersonViewer::GetPos(const Projection& /* unused*/) const { 72 | return pos_; 73 | } 74 | 75 | void FirstPersonViewer::SetFov(float fov) { 76 | fov_ = fov; 77 | } 78 | 79 | void FirstPersonViewer::SetAspect(float aspect) { 80 | aspect_ = aspect; 81 | } 82 | 83 | Vector3f FirstPersonViewer::GetDirection() const { 84 | return Vector3f::FromYawPitch(yaw_, pitch_); 85 | } 86 | 87 | void FirstPersonViewer::SetPos(Vector3i pos) { 88 | pos_ = pos; 89 | 90 | FixPosition(); 91 | } 92 | 93 | void FirstPersonViewer::Move(int flags, float speed, float time) { 94 | /* 1 meter direction-collinear vector in OSM coordinate space */ 95 | Vector3f dirbasis = Vector3f( 96 | GEOM_LONSPAN / WGS84_EARTH_EQ_LENGTH / cos(pos_.y * GEOM_DEG_TO_RAD), 97 | GEOM_LONSPAN / WGS84_EARTH_EQ_LENGTH, 98 | GEOM_UNITSINMETER 99 | ); 100 | 101 | Vector3f dir = GetDirection(); 102 | Vector3f worldup = Vector3f(0.0f, 0.0f, 1.0f); 103 | Vector3f right = dir.CrossProduct(worldup).Normalized(); 104 | Vector3f relup = right.CrossProduct(dir).Normalized(); 105 | 106 | if (flags & FORWARD) 107 | pos_ += dirbasis * dir * speed * time; 108 | if (flags & BACKWARD) 109 | pos_ -= dirbasis * dir * speed * time; 110 | if (flags & LEFT) 111 | pos_ -= dirbasis * right * speed * time; 112 | if (flags & RIGHT) 113 | pos_ += dirbasis * right * speed * time; 114 | if (flags & UP) 115 | pos_ += dirbasis * relup * speed * time; 116 | if (flags & DOWN) 117 | pos_ -= dirbasis * relup * speed * time; 118 | if (flags & HIGHER) 119 | pos_ += dirbasis * worldup * speed * time; 120 | if (flags & LOWER) 121 | pos_ -= dirbasis * worldup * speed * time; 122 | 123 | FixPosition(); 124 | } 125 | 126 | void FirstPersonViewer::FixPosition() { 127 | /* Update landscape height */ 128 | if (heightmap_) 129 | landscape_height_ = heightmap_->GetHeight(Vector2d(pos_.x, pos_.y)); 130 | 131 | /* Wrap around */ 132 | if (pos_.x > GEOM_MAXLON) 133 | pos_.x -= GEOM_LONSPAN; 134 | if (pos_.x < GEOM_MINLON) 135 | pos_.x += GEOM_LONSPAN; 136 | 137 | /* Limit poles */ 138 | if (pos_.y > GEOM_MERCATOR_MAXLAT) 139 | pos_.y = GEOM_MERCATOR_MAXLAT; 140 | if (pos_.y < GEOM_MERCATOR_MINLAT) 141 | pos_.y = GEOM_MERCATOR_MINLAT; 142 | 143 | /* Limit height */ 144 | if (pos_.z < landscape_height_ + 1) 145 | pos_.z = landscape_height_ + 1; 146 | if (pos_.z > std::numeric_limits::max()) 147 | pos_.z = std::numeric_limits::max(); 148 | } 149 | 150 | void FirstPersonViewer::FixRotation() { 151 | static const float PitchLimit = M_PI/2.0*0.9; 152 | 153 | if (pitch_ > PitchLimit) 154 | pitch_ = PitchLimit; 155 | if (pitch_ < -PitchLimit) 156 | pitch_ = -PitchLimit; 157 | if (yaw_ > M_PI) 158 | yaw_ -= M_PI*2.0; 159 | if (yaw_ < -M_PI) 160 | yaw_ += M_PI*2.0; 161 | } 162 | 163 | void FirstPersonViewer::SetRotation(float yaw, float pitch) { 164 | yaw_ = yaw; 165 | pitch_ = pitch; 166 | 167 | FixRotation(); 168 | } 169 | 170 | void FirstPersonViewer::Rotate(float yawspeed, float pitchspeed, float time) { 171 | yaw_ += yawspeed * time; 172 | pitch_ += pitchspeed * time; 173 | 174 | FixRotation(); 175 | } 176 | 177 | float FirstPersonViewer::GetYaw() const { 178 | return yaw_; 179 | } 180 | 181 | float FirstPersonViewer::GetPitch() const { 182 | return pitch_; 183 | } 184 | 185 | float FirstPersonViewer::GetFov() const { 186 | return fov_; 187 | } 188 | 189 | float FirstPersonViewer::GetAspect() const { 190 | return aspect_; 191 | } 192 | 193 | Vector3d& FirstPersonViewer::MutablePos() { 194 | return pos_; 195 | } 196 | 197 | void FirstPersonViewer::SetHeightmapDatasource(const HeightmapDatasource* heightmap) { 198 | heightmap_ = heightmap; 199 | } 200 | -------------------------------------------------------------------------------- /libglosm-client/GPXLayer.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #include 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | #include 30 | 31 | GPXLayer::GPXLayer(const Projection projection, const GPXDatasource& datasource, const HeightmapDatasource& heightmap): TileManager(projection), projection_(projection), datasource_(datasource), heightmap_(heightmap) { 32 | } 33 | 34 | GPXLayer::~GPXLayer() { 35 | } 36 | 37 | void GPXLayer::Render(const Viewer& viewer) { 38 | /* Setup projection */ 39 | viewer.SetupViewerMatrix(projection_); 40 | 41 | /* OpenGL attrs */ 42 | glMatrixMode(GL_MODELVIEW); 43 | glEnable(GL_BLEND); 44 | glEnable(GL_CULL_FACE); 45 | glEnable(GL_DEPTH_TEST); 46 | glShadeModel(GL_FLAT); 47 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 48 | 49 | /* Lighting */ 50 | GLfloat global_ambient[] = {0.0, 0.0, 0.0, 1.0}; 51 | GLfloat light_position[] = {-0.2, -0.777, 0.63, 0.0}; 52 | GLfloat light_diffuse[] = {0.45, 0.45, 0.45, 1.0}; 53 | GLfloat light_ambient[] = {0.33, 0.33, 0.33, 1.0}; 54 | GLfloat material_diffuse[] = {1.0, 1.0, 1.0, 0.9}; 55 | 56 | glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient); 57 | 58 | glLightfv(GL_LIGHT0, GL_POSITION, light_position); 59 | glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); 60 | glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); 61 | 62 | glMaterialfv(GL_FRONT, GL_AMBIENT, material_diffuse); 63 | glMaterialfv(GL_FRONT, GL_DIFFUSE, material_diffuse); 64 | 65 | /* Render tile(s) */ 66 | TileManager::Render(viewer); 67 | } 68 | 69 | Tile* GPXLayer::SpawnTile(const BBoxi& bbox, int flags) const { 70 | return new GPXTile(projection_, datasource_, heightmap_, bbox.GetCenter(), bbox); 71 | } 72 | -------------------------------------------------------------------------------- /libglosm-client/GPXTile.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #include 22 | 23 | #include 24 | #include 25 | 26 | #include 27 | 28 | #include 29 | #include 30 | 31 | GPXTile::GPXTile(const Projection& projection, const GPXDatasource& datasource, const HeightmapDatasource& heightmap, const Vector2i& ref, const BBoxi& bbox) : Tile(ref), size_(0) { 32 | std::vector points; 33 | datasource.GetPoints(points, bbox); 34 | 35 | if (!points.empty()) { 36 | points_.reset(new VertexBuffer(GL_ARRAY_BUFFER)); 37 | 38 | points_->Data().reserve(2 * points.size()); 39 | for (std::vector::const_iterator i = points.begin(); i != points.end(); ++i) { 40 | points_->Data().push_back(projection.Project(*i, ref)); 41 | points_->Data().push_back(projection.Project(Vector3i(i->x, i->y, heightmap.GetHeight(*i)), ref)); 42 | } 43 | 44 | size_ = points_->GetFootprint(); 45 | } 46 | } 47 | 48 | GPXTile::~GPXTile() { 49 | } 50 | 51 | void GPXTile::Render() { 52 | if (points_.get()) { 53 | glDepthFunc(GL_LEQUAL); 54 | 55 | glColor4f(1.0f, 0.0f, 1.0f, 0.5f); 56 | glPointSize(3.0); 57 | 58 | points_->Bind(); 59 | 60 | glEnableClientState(GL_VERTEX_ARRAY); 61 | 62 | glVertexPointer(3, GL_FLOAT, sizeof(Vector3f)*2, BUFFER_OFFSET(0)); 63 | glDrawArrays(GL_POINTS, 0, points_->GetSize()/2); 64 | 65 | glVertexPointer(3, GL_FLOAT, sizeof(Vector3f), BUFFER_OFFSET(0)); 66 | glDrawArrays(GL_LINES, 0, points_->GetSize()); 67 | 68 | glDisableClientState(GL_VERTEX_ARRAY); 69 | } 70 | } 71 | 72 | size_t GPXTile::GetSize() const { 73 | return size_; 74 | } 75 | -------------------------------------------------------------------------------- /libglosm-client/GeometryLayer.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #include 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | #include 30 | 31 | GeometryLayer::GeometryLayer(const Projection projection, const GeometryDatasource& datasource): TileManager(projection), projection_(projection), datasource_(datasource) { 32 | } 33 | 34 | GeometryLayer::~GeometryLayer() { 35 | } 36 | 37 | void GeometryLayer::Render(const Viewer& viewer) { 38 | /* Setup projection */ 39 | viewer.SetupViewerMatrix(projection_); 40 | 41 | /* OpenGL attrs */ 42 | glMatrixMode(GL_MODELVIEW); 43 | glEnable(GL_BLEND); 44 | glEnable(GL_CULL_FACE); 45 | glEnable(GL_DEPTH_TEST); 46 | glShadeModel(GL_FLAT); 47 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 48 | 49 | /* Lighting */ 50 | GLfloat global_ambient[] = {0.0, 0.0, 0.0, 1.0}; 51 | GLfloat light_position[] = {-0.2, -0.777, 0.63, 0.0}; 52 | GLfloat light_diffuse[] = {0.45, 0.45, 0.45, 1.0}; 53 | GLfloat light_ambient[] = {0.33, 0.33, 0.33, 1.0}; 54 | GLfloat material_diffuse[] = {1.0, 1.0, 1.0, 0.9}; 55 | 56 | glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient); 57 | 58 | glLightfv(GL_LIGHT0, GL_POSITION, light_position); 59 | glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); 60 | glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); 61 | 62 | glMaterialfv(GL_FRONT, GL_AMBIENT, material_diffuse); 63 | glMaterialfv(GL_FRONT, GL_DIFFUSE, material_diffuse); 64 | 65 | /* Render tile(s) */ 66 | TileManager::Render(viewer); 67 | } 68 | 69 | Tile* GeometryLayer::SpawnTile(const BBoxi& bbox, int flags) const { 70 | Geometry geom; 71 | datasource_.GetGeometry(geom, bbox, flags); 72 | return new GeometryTile(projection_, geom, bbox.GetCenter(), bbox); 73 | } 74 | -------------------------------------------------------------------------------- /libglosm-client/GeometryTile.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #include 22 | 23 | #include 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | GeometryTile::GeometryTile(const Projection& projection, const Geometry& geometry, const Vector2i& ref, const BBoxi& bbox) : Tile(ref), size_(0) { 30 | if (!geometry.GetLinesLengths().empty()) { 31 | lines_vertices_.reset(new VertexBuffer(GL_ARRAY_BUFFER)); 32 | lines_indices_.reset(new VertexBuffer(GL_ELEMENT_ARRAY_BUFFER)); 33 | 34 | const Geometry::VertexVector& vertices = geometry.GetLinesVertices(); 35 | const Geometry::LengthVector& lengths = geometry.GetLinesLengths(); 36 | 37 | lines_vertices_->Data().reserve(vertices.size()); 38 | 39 | for (unsigned int i = 0, curpos = 0; i < lengths.size(); ++i) { 40 | #if defined(WITH_GLES) 41 | /* GL ES doesn't support VBOs larger than 65536 elements */ 42 | /* @todo split into multiple VBOs */ 43 | if (curpos + lengths[i] > 65536) 44 | break; 45 | #endif 46 | 47 | for (int j = 0; j < lengths[i]; ++j) { 48 | lines_vertices_->Data().push_back(projection.Project(vertices[curpos + j], ref)); 49 | } 50 | 51 | for (int j = 1; j < lengths[i]; ++j) { 52 | lines_indices_->Data().push_back(curpos + j - 1); 53 | lines_indices_->Data().push_back(curpos + j); 54 | } 55 | 56 | curpos += lengths[i]; 57 | } 58 | 59 | size_ += lines_vertices_->GetFootprint() + lines_indices_->GetFootprint(); 60 | } 61 | 62 | if (!geometry.GetConvexLengths().empty()) { 63 | convex_vertices_.reset(new VertexBuffer(GL_ARRAY_BUFFER)); 64 | convex_indices_.reset(new VertexBuffer(GL_ELEMENT_ARRAY_BUFFER)); 65 | 66 | const Geometry::VertexVector& vertices = geometry.GetConvexVertices(); 67 | const Geometry::LengthVector& lengths = geometry.GetConvexLengths(); 68 | 69 | convex_vertices_->Data().reserve(vertices.size()); 70 | 71 | for (unsigned int i = 0, curpos = 0; i < lengths.size(); ++i) { 72 | #if defined(WITH_GLES) 73 | /* GL ES doesn't support VBOs larger than 65536 elements */ 74 | /* @todo split into multiple VBOs */ 75 | if (curpos + lengths[i] > 65536) 76 | break; 77 | #endif 78 | 79 | for (int j = 0; j < lengths[i]; ++j) { 80 | convex_vertices_->Data().push_back(Vertex(projection.Project(vertices[curpos + j], ref))); 81 | } 82 | 83 | for (int j = 2; j < lengths[i]; ++j) { 84 | convex_indices_->Data().push_back(curpos); 85 | convex_indices_->Data().push_back(curpos + j - 1); 86 | convex_indices_->Data().push_back(curpos + j); 87 | } 88 | 89 | CalcFanNormal(&convex_vertices_->Data()[curpos], lengths[i]); 90 | 91 | curpos += lengths[i]; 92 | } 93 | 94 | size_ += convex_vertices_->GetFootprint() + convex_indices_->GetFootprint(); 95 | } 96 | } 97 | 98 | GeometryTile::~GeometryTile() { 99 | } 100 | 101 | void GeometryTile::CalcFanNormal(Vertex* vertices, int count) { 102 | Vector3f first = vertices[1].pos - vertices[0].pos; 103 | Vector3f normal; 104 | for (int i = 1; i < count; ++i) { 105 | Vector3f v = vertices[i].pos - vertices[0].pos; 106 | 107 | if ((normal = first.CrossProduct(v)).LengthSquare() > 0) 108 | break; 109 | } 110 | normal.Normalize(); 111 | 112 | for (int i = 0; i < count; ++i) 113 | vertices[i].norm = normal; 114 | } 115 | 116 | void GeometryTile::Render() { 117 | if (lines_vertices_.get()) { 118 | glColor4f(0.0f, 0.0f, 0.0f, 0.5f); 119 | 120 | lines_vertices_->Bind(); 121 | 122 | glEnableClientState(GL_VERTEX_ARRAY); 123 | glVertexPointer(3, GL_FLOAT, sizeof(Vector3f), BUFFER_OFFSET(0)); 124 | 125 | // lines_indices_->Bind(); 126 | // glDrawElements(GL_LINES, lines_indices_->GetSize(), GL_UNSIGNED_INT, 0); 127 | glDrawElements(GL_LINES, lines_indices_->GetSize(), GL_UNSIGNED_INT, lines_indices_->Data().data()); 128 | // lines_indices_->UnBind(); 129 | 130 | glDisableClientState(GL_VERTEX_ARRAY); 131 | } 132 | 133 | if (convex_vertices_.get()) { 134 | /* zpass */ 135 | /*glColor4f(0.0f, 0.0f, 0.0f, 0.0f); 136 | triangles_->Render(); 137 | quads_->Render(); 138 | 139 | glDepthFunc(GL_EQUAL);*/ 140 | 141 | /* objects */ 142 | glEnable(GL_LIGHTING); 143 | glEnable(GL_LIGHT0); 144 | 145 | convex_vertices_->Bind(); 146 | 147 | glEnableClientState(GL_VERTEX_ARRAY); 148 | glVertexPointer(3, GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(0)); 149 | 150 | glEnableClientState(GL_NORMAL_ARRAY); 151 | glNormalPointer(GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(12)); 152 | 153 | /* XXX: since we can't do PolygonOffset for lines, we offset all polygons instead */ 154 | glPolygonOffset(1.0, 1.0); 155 | glEnable(GL_POLYGON_OFFSET_FILL); 156 | 157 | // convex_indices_->Bind(); 158 | // glDrawElements(GL_TRIANGLES, convex_indices_->GetSize(), GL_UNSIGNED_INT, 0); 159 | glDrawElements(GL_TRIANGLES, convex_indices_->GetSize(), GL_UNSIGNED_INT, convex_indices_->Data().data()); 160 | // convex_indices_->UnBind(); 161 | 162 | glDisable(GL_POLYGON_OFFSET_FILL); 163 | 164 | glDisableClientState(GL_NORMAL_ARRAY); 165 | glDisableClientState(GL_VERTEX_ARRAY); 166 | 167 | glDisable(GL_LIGHT0); 168 | glDisable(GL_LIGHTING); 169 | } 170 | } 171 | 172 | size_t GeometryTile::GetSize() const { 173 | return size_; 174 | } 175 | -------------------------------------------------------------------------------- /libglosm-client/MercatorProjection.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #include 22 | 23 | #include 24 | 25 | #include 26 | 27 | MercatorProjection::MercatorProjection() : Projection(&ProjectImpl, &UnProjectImpl) { 28 | } 29 | 30 | Vector3f MercatorProjection::ProjectImpl(const Vector3i& point, const Vector3i& ref) { 31 | return Vector3f( 32 | (double)((osmlong_t)point.x - ref.x) * GEOM_DEG_TO_RAD, 33 | mercator((double)point.y * GEOM_DEG_TO_RAD) - mercator((double)ref.y * GEOM_DEG_TO_RAD), 34 | (double)(point.z - ref.z) / GEOM_UNITSINMETER / (WGS84_EARTH_EQ_RADIUS * cos((double)(point.y * GEOM_DEG_TO_RAD))) 35 | ); 36 | } 37 | 38 | Vector3i MercatorProjection::UnProjectImpl(const Vector3f& point, const Vector3i& ref) { 39 | double y = unmercator((double)point.y + mercator((double)ref.y * GEOM_DEG_TO_RAD)); 40 | return Vector3i( 41 | ref.x + (osmint_t)round(point.x * GEOM_RAD_TO_DEG), 42 | (osmint_t)round(y * GEOM_RAD_TO_DEG), 43 | ref.z + (osmint_t)round((double)point.z * GEOM_UNITSINMETER * WGS84_EARTH_EQ_RADIUS * cos(y)) 44 | ); 45 | } 46 | -------------------------------------------------------------------------------- /libglosm-client/OrthoViewer.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #include 22 | 23 | #include 24 | #include 25 | 26 | #include 27 | 28 | OrthoViewer::OrthoViewer() : bbox_(BBoxi::ForEarth()), skew_(0.0f) { 29 | } 30 | 31 | void OrthoViewer::SetupViewerMatrix(const Projection& projection) const { 32 | Vector3i center = GetPos(projection); 33 | 34 | float xspan = fabs(projection.Project(Vector2i(bbox_.left, 0), center).x); 35 | float yspan = fabs(projection.Project(Vector2i(0, bbox_.top), center).y); 36 | 37 | /* Take +/- 1km as Z range */ 38 | float zspan = fabs(projection.Project(Vector3i(center.x, center.y, 1000 * GEOM_UNITSINMETER), Vector3i(center.x, center.y, 0)).z); 39 | 40 | /* undefined which is aspect: x/y or y/x */ 41 | float perspective[] = { 42 | 1.0f/xspan, 0.0f, 0.0f, 0.0f, 43 | 0.0f, 1.0f/yspan, 0.0f, 0.0f, 44 | 0.0f, skew_/yspan, -1.0/zspan, 0.0f, 45 | 0.0f, 0.0f, 0.0f, 1.0f, 46 | }; 47 | 48 | glMatrixMode(GL_PROJECTION); 49 | glLoadMatrixf(perspective); 50 | } 51 | 52 | Vector3i OrthoViewer::GetPos(const Projection& projection) const { 53 | /* `naive' center that doesn't take projection into account */ 54 | Vector2i nearcenter = bbox_.GetCenter(); 55 | 56 | /* real center that takes projectino into account; to minimize error, calculated relative to nearcenter */ 57 | return projection.UnProject((projection.Project(bbox_.GetBottomLeft(), nearcenter) + projection.Project(bbox_.GetTopRight(), nearcenter))/2.0, nearcenter); 58 | } 59 | 60 | void OrthoViewer::SetBBox(const BBoxi& bbox) { 61 | bbox_ = bbox; 62 | } 63 | 64 | void OrthoViewer::SetSkew(float skew) { 65 | skew_ = skew; 66 | } 67 | -------------------------------------------------------------------------------- /libglosm-client/Projection.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #include 22 | 23 | Projection::Projection(ProjectFunction pf, UnProjectFunction uf): project_(pf), unproject_(uf) { 24 | } 25 | 26 | Vector3f Projection::Project(const Vector3i& point, const Vector3i& ref) const { 27 | return project_(point, ref); 28 | } 29 | 30 | Vector3i Projection::UnProject(const Vector3f& point, const Vector3i& ref) const { 31 | return unproject_(point, ref); 32 | } 33 | 34 | void Projection::ProjectPoints(const std::vector& in, const Vector3i& ref, std::vector& out) const { 35 | /* "slow" fallback implementation; though virtual calls 36 | * add overhead, it's too small (~3%) to even care; but this 37 | * may be redefined in subclasses */ 38 | out.reserve(out.size() + in.size()); 39 | for (std::vector::const_iterator i = in.begin(); i != in.end(); ++i) 40 | out.push_back(project_(*i, ref)); 41 | } 42 | -------------------------------------------------------------------------------- /libglosm-client/SphericalProjection.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #include 22 | 23 | #include 24 | 25 | #include 26 | 27 | SphericalProjection::SphericalProjection() : Projection(&ProjectImpl, &UnProjectImpl) { 28 | } 29 | 30 | Vector3f SphericalProjection::ProjectImpl(const Vector3i& point, const Vector3i& ref) { 31 | double point_angle_x = ((double)point.x - (double)ref.x) * GEOM_DEG_TO_RAD; 32 | double point_angle_y = (double)point.y * GEOM_DEG_TO_RAD; 33 | double point_height = (double)point.z / GEOM_UNITSINMETER; 34 | 35 | double ref_angle_y = (double)ref.y * GEOM_DEG_TO_RAD; 36 | double ref_height = (double)ref.z / GEOM_UNITSINMETER; 37 | 38 | #ifdef HAVE_SINCOS 39 | double sinx, cosx, siny, cosy; 40 | sincos(point_angle_x, &sinx, &cosx); 41 | sincos(point_angle_y, &siny, &cosy); 42 | #else 43 | double cosx = cos(point_angle_x); 44 | double cosy = cos(point_angle_y); 45 | double sinx = sin(point_angle_x); 46 | double siny = sin(point_angle_y); 47 | #endif 48 | Vector3d point_vector( 49 | (WGS84_EARTH_EQ_RADIUS + point_height) * sinx * cosy, 50 | (WGS84_EARTH_EQ_RADIUS + point_height) * siny, 51 | (WGS84_EARTH_EQ_RADIUS + point_height) * cosx * cosy 52 | ); 53 | 54 | #ifdef HAVE_SINCOS 55 | double sinay, cosay; 56 | sincos(ref_angle_y, &sinay, &cosay); 57 | #else 58 | double sinay = sin(ref_angle_y); 59 | double cosay = cos(ref_angle_y); 60 | #endif 61 | return Vector3f( 62 | point_vector.x, 63 | point_vector.y * cosay - point_vector.z * sinay, 64 | point_vector.y * sinay + point_vector.z * cosay - WGS84_EARTH_EQ_RADIUS - ref_height 65 | ); 66 | } 67 | 68 | Vector3i SphericalProjection::UnProjectImpl(const Vector3f& point, const Vector3i& ref) { 69 | double ref_angle_y = (double)ref.y * GEOM_DEG_TO_RAD; 70 | double ref_height = (double)ref.z / GEOM_UNITSINMETER; 71 | 72 | #ifdef HAVE_SINCOS 73 | double sinay, cosay; 74 | sincos(ref_angle_y, &sinay, &cosay); 75 | #else 76 | double sinay = sin(ref_angle_y); 77 | double cosay = cos(ref_angle_y); 78 | #endif 79 | Vector3d point_vector( 80 | point.x, 81 | sinay * (WGS84_EARTH_EQ_RADIUS + ref_height + point.z) + cosay * point.y, 82 | cosay * (WGS84_EARTH_EQ_RADIUS + ref_height + point.z) - sinay * point.y 83 | ); 84 | 85 | Vector3d spherical( 86 | atan2(point_vector.x, point_vector.z), 87 | atan2(point_vector.y, sqrt(point_vector.x * point_vector.x + point_vector.z * point_vector.z)), 88 | point_vector.Length() - WGS84_EARTH_EQ_RADIUS 89 | ); 90 | 91 | return Vector3i( 92 | ref.x + (osmint_t)round(spherical.x * GEOM_RAD_TO_DEG), 93 | (osmint_t)round(spherical.y * GEOM_RAD_TO_DEG), 94 | (osmint_t)round(spherical.z * GEOM_UNITSINMETER) 95 | ); 96 | } 97 | -------------------------------------------------------------------------------- /libglosm-client/TerrainLayer.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #include 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #include 29 | 30 | TerrainLayer::TerrainLayer(const Projection projection, HeightmapDatasource& datasource): TileManager(projection), projection_(projection), datasource_(datasource) { 31 | } 32 | 33 | TerrainLayer::~TerrainLayer() { 34 | } 35 | 36 | void TerrainLayer::Render(const Viewer& viewer) { 37 | /* Setup projection */ 38 | viewer.SetupViewerMatrix(projection_); 39 | 40 | /* OpenGL attrs */ 41 | glMatrixMode(GL_MODELVIEW); 42 | glEnable(GL_BLEND); 43 | glEnable(GL_CULL_FACE); 44 | glEnable(GL_DEPTH_TEST); 45 | glShadeModel(GL_SMOOTH); 46 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 47 | 48 | /* Lighting */ 49 | GLfloat global_ambient[] = {0.0, 0.0, 0.0, 1.0}; 50 | GLfloat light_position[] = {-0.2, -0.777, 0.63, 0.0}; 51 | GLfloat light_diffuse[] = {0.45, 0.45, 0.45, 1.0}; 52 | GLfloat light_ambient[] = {0.33, 0.33, 0.33, 1.0}; 53 | float l = 0.85; 54 | GLfloat material_diffuse[] = {l, l, l, 1.0}; 55 | 56 | glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient); 57 | 58 | glLightfv(GL_LIGHT0, GL_POSITION, light_position); 59 | glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); 60 | glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); 61 | 62 | glMaterialfv(GL_FRONT, GL_AMBIENT, material_diffuse); 63 | glMaterialfv(GL_FRONT, GL_DIFFUSE, material_diffuse); 64 | 65 | glEnable(GL_LIGHTING); 66 | glEnable(GL_LIGHT0); 67 | 68 | /* Render tile(s) */ 69 | 70 | /* XXX: we use 2 here as 1 is used for geometry */ 71 | glPolygonOffset(2.0, 2.0); 72 | glEnable(GL_POLYGON_OFFSET_FILL); 73 | 74 | TileManager::Render(viewer); 75 | 76 | glDisable(GL_POLYGON_OFFSET_FILL); 77 | 78 | glDisable(GL_LIGHT0); 79 | glDisable(GL_LIGHTING); 80 | } 81 | 82 | Tile* TerrainLayer::SpawnTile(const BBoxi& bbox, int flags) const { 83 | return new TerrainTile(projection_, datasource_, bbox.GetCenter(), bbox); 84 | } 85 | -------------------------------------------------------------------------------- /libglosm-client/TerrainTile.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #include 22 | 23 | #include 24 | #include 25 | 26 | #include 27 | #include 28 | 29 | #include 30 | #include 31 | 32 | TerrainTile::TerrainTile(const Projection& projection, HeightmapDatasource& datasource, const Vector2i& ref, const BBoxi& bbox) : Tile(ref) { 33 | HeightmapDatasource::Heightmap heightmap; 34 | 35 | /* we request heightmap with extra 1-point margin so we can 36 | * calculate correct normals for edge vertices */ 37 | datasource.GetHeightmap(bbox, 1, heightmap); 38 | 39 | int width = heightmap.width - 2; 40 | int height = heightmap.height - 2; 41 | 42 | /* for each tile, we store position and normal for each 43 | * vertex in the grid; we also store index array which 44 | * arranges vertices into a single triangle strip */ 45 | 46 | vbo_.reset(new VertexBuffer(GL_ARRAY_BUFFER)); 47 | vbo_->Data().resize(width * height); 48 | 49 | /* temporary array of projected points */ 50 | std::vector projected; 51 | projected.reserve(heightmap.width * heightmap.height); 52 | for (int y = 0; y < heightmap.height; ++y) { 53 | for (int x = 0; x < heightmap.width; ++x) { 54 | projected.push_back(projection.Project(Vector3i( 55 | (osmint_t)((double)heightmap.bbox.left) + ((double)heightmap.bbox.right - (double)heightmap.bbox.left) * ((double)x / (double)(heightmap.width - 1)), 56 | (osmint_t)((double)heightmap.bbox.bottom) + ((double)heightmap.bbox.top - (double)heightmap.bbox.bottom) * ((double)y / (double)(heightmap.height - 1)), 57 | heightmap.points[y * heightmap.width + x] 58 | ), ref)); 59 | } 60 | } 61 | 62 | /* prepare vertices & normals */ 63 | int n = 0; 64 | for (int y = 1; y < heightmap.height - 1; ++y) { 65 | for (int x = 1; x < heightmap.width - 1; ++x) { 66 | Vector3f v1 = projected[y * heightmap.width + x + 1] - projected[y * heightmap.width + x - 1]; 67 | Vector3f v2 = projected[(y + 1) * heightmap.width + x] - projected[(y - 1) * heightmap.width + x]; 68 | 69 | vbo_->Data()[n].pos = projected[y * heightmap.width + x]; 70 | vbo_->Data()[n].norm = v1.CrossProduct(v2).Normalized(); 71 | 72 | n++; 73 | } 74 | } 75 | 76 | /* clamp grid edges with tile bounds */ 77 | 78 | double k1, k2; 79 | double cellheight = ((double)heightmap.bbox.top - (double)heightmap.bbox.bottom) / (double)(heightmap.height - 1); 80 | double cellwidth = ((double)heightmap.bbox.right - (double)heightmap.bbox.left) / (double)(heightmap.width - 1); 81 | 82 | /* 83 | * @todo this clamping is not quite correct: it doesn't 84 | * take the fact that terrain grid consists of triangles 85 | * into account 86 | */ 87 | 88 | /* clamp bottom & top */ 89 | k1 = ((double)bbox.bottom - (double)heightmap.bbox.bottom) / cellheight - 1.0; 90 | k2 = ((double)heightmap.bbox.top - (double)bbox.top) / cellheight - 1.0; 91 | for (int x = 0; x < width; ++x) { 92 | vbo_->Data()[x].pos = vbo_->Data()[x].pos * (1.0 - k1) + vbo_->Data()[width + x].pos * (k1); 93 | vbo_->Data()[x].norm = vbo_->Data()[x].norm * (1.0 - k1) + vbo_->Data()[width + x].norm * (k1); 94 | 95 | vbo_->Data()[(height - 1) * width + x].pos = vbo_->Data()[(height - 1) * width + x].pos * (1.0 - k2) + vbo_->Data()[(height - 2) * width + x].pos * (k2); 96 | vbo_->Data()[(height - 1) * width + x].norm = vbo_->Data()[(height - 1) * width + x].norm * (1.0 - k2) + vbo_->Data()[(height - 2) * width + x].norm * (k2); 97 | } 98 | 99 | /* clamp left & right */ 100 | k1 = ((double)bbox.left - (double)heightmap.bbox.left) / cellwidth - 1.0; 101 | k2 = ((double)heightmap.bbox.right - (double)bbox.right) / cellwidth - 1.0; 102 | for (int y = 0; y < height; ++y) { 103 | vbo_->Data()[y * width].pos = vbo_->Data()[y * width].pos * (1.0 - k1) + vbo_->Data()[y * width + 1].pos * (k1); 104 | vbo_->Data()[y * width].norm = vbo_->Data()[y * width].norm * (1.0 - k1) + vbo_->Data()[y * width + 1].norm * (k1); 105 | 106 | vbo_->Data()[y * width + width - 1].pos = vbo_->Data()[y * width + width - 1].pos * (1.0 - k2) + vbo_->Data()[y * width + width - 2].pos * (k2); 107 | vbo_->Data()[y * width + width - 1].norm = vbo_->Data()[y * width + width - 1].norm * (1.0 - k2) + vbo_->Data()[y * width + width - 2].norm * (k2); 108 | } 109 | 110 | /* ought to be enough for anybody? test with hires hawaii heightmaps */ 111 | if (vbo_->GetSize() > 65536) 112 | throw std::logic_error("error constructing TerrainTile: attempt to store more than 65536 vertices in a VBO indexed with SHORTs"); 113 | 114 | /* prepare indices */ 115 | ibo_.reset(new VertexBuffer(GL_ELEMENT_ARRAY_BUFFER)); 116 | ibo_->Data().reserve((height - 1) * (width * 2 + 2) - 2); 117 | for (int y = 0; y < height - 1; ++y) { 118 | /* since strip is arranged per-row, we duplicate last and first 119 | * vertices in each row to make triangle between rows degraded 120 | * and thus not renderable */ 121 | if (y > 0) 122 | ibo_->Data().push_back((y + 1) * width); 123 | for (int x = 0; x < width; ++x) { 124 | ibo_->Data().push_back((y + 1) * width + x); 125 | ibo_->Data().push_back(y * width + x); 126 | } 127 | if (y < height - 2) 128 | ibo_->Data().push_back(y * width + width - 1); 129 | } 130 | 131 | size_ = vbo_->GetFootprint() + ibo_->GetFootprint(); 132 | } 133 | 134 | TerrainTile::~TerrainTile() { 135 | } 136 | 137 | void TerrainTile::Render() { 138 | vbo_->Bind(); 139 | 140 | glEnableClientState(GL_VERTEX_ARRAY); 141 | glVertexPointer(3, GL_FLOAT, sizeof(TerrainVertex), BUFFER_OFFSET(0)); 142 | 143 | glEnableClientState(GL_NORMAL_ARRAY); 144 | glNormalPointer(GL_FLOAT, sizeof(TerrainVertex), BUFFER_OFFSET(12)); 145 | 146 | ibo_->Bind(); 147 | 148 | glDrawElements(GL_TRIANGLE_STRIP, ibo_->GetSize(), GL_UNSIGNED_SHORT, BUFFER_OFFSET(0)); 149 | 150 | ibo_->UnBind(); 151 | 152 | glDisableClientState(GL_VERTEX_ARRAY); 153 | glDisableClientState(GL_NORMAL_ARRAY); 154 | } 155 | 156 | size_t TerrainTile::GetSize() const { 157 | return size_; 158 | } 159 | -------------------------------------------------------------------------------- /libglosm-client/glosm/CheckGL.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef CHECKGL_HH 22 | #define CHECKGL_HH 23 | 24 | #include 25 | 26 | /** 27 | * Exception that denotes unsupported OpenGL function 28 | */ 29 | class GLUnsupportedException: public Exception { 30 | }; 31 | 32 | /** 33 | * Checks whether OpenGL functions required by glosm are available 34 | * 35 | * Throws exception in case of error 36 | */ 37 | void CheckGL(); 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /libglosm-client/glosm/FirstPersonViewer.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef FIRSTPERSONVIEWER_HH 22 | #define FIRSTPERSONVIEWER_HH 23 | 24 | #include 25 | 26 | class Projection; 27 | class HeightmapDatasource; 28 | 29 | /** 30 | * Viewer describing perspective projection. 31 | */ 32 | class FirstPersonViewer : public Viewer { 33 | public: 34 | /** 35 | * Movement direction flags. 36 | * 37 | * These are used to describe in which way (relative to 38 | * look direction) a viewer should move to 39 | * 40 | * LOWER/HIGHER values are actually absolute up/down. 41 | */ 42 | enum Direction { 43 | FORWARD = 0x0001, 44 | BACKWARD = 0x0002, 45 | LEFT = 0x0004, 46 | RIGHT = 0x0008, 47 | UP = 0x0010, 48 | DOWN = 0x0020, 49 | LOWER = 0x0040, 50 | HIGHER = 0x0080, 51 | }; 52 | 53 | protected: 54 | Vector3f GetDirection() const; 55 | void FixRotation(); 56 | void FixPosition(); 57 | 58 | public: 59 | FirstPersonViewer(); 60 | FirstPersonViewer(const Vector3i& pos); 61 | FirstPersonViewer(const Vector3i& pos, float yaw, float pitch); 62 | 63 | virtual void SetupViewerMatrix(const Projection& projection) const; 64 | virtual Vector3i GetPos(const Projection& projection) const; 65 | 66 | void SetFov(float fov); 67 | void SetAspect(float aspect); 68 | 69 | void SetPos(Vector3i pos); 70 | 71 | /** 72 | * Move viewer smoothly 73 | * 74 | * @param flags combination of Direction constants which 75 | * represents direction for viewer to move to 76 | * @param speed movement speed in meters/second 77 | * @param time time delta for movement in seconds 78 | */ 79 | void Move(int flags, float speed, float time); 80 | 81 | void SetRotation(float yaw, float pitch); 82 | 83 | void Rotate(float yawspeed, float pitchspeed, float time); 84 | 85 | float GetPitch() const; 86 | float GetYaw() const; 87 | 88 | float GetFov() const; 89 | float GetAspect() const; 90 | 91 | Vector3d& MutablePos(); 92 | 93 | void SetHeightmapDatasource(const HeightmapDatasource* heightmap); 94 | 95 | protected: 96 | const HeightmapDatasource* heightmap_; 97 | 98 | protected: 99 | /** Eye position */ 100 | Vector3d pos_; 101 | 102 | /** Terrain elevation at eye point */ 103 | osmint_t landscape_height_; 104 | 105 | float yaw_; 106 | float pitch_; 107 | 108 | float fov_; 109 | float aspect_; 110 | }; 111 | 112 | #endif 113 | -------------------------------------------------------------------------------- /libglosm-client/glosm/GPXLayer.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef GPXLAYER_HH 22 | #define GPXLAYER_HH 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | class Viewer; 30 | class GPXDatasource; 31 | class HeightmapDatasource; 32 | 33 | /** 34 | * Layer with 3D OpenStreetMap data. 35 | */ 36 | class GPXLayer : public Layer, public TileManager, private NonCopyable { 37 | public: 38 | enum Mode { 39 | NORMAL = 0, /* use real track elevations */ 40 | FLAT = 1, /* place points on ground, don't use track elevations */ 41 | SUBSTRACT = 2, /* substract heightmap from track elevation */ 42 | }; 43 | 44 | protected: 45 | const Projection projection_; 46 | const GPXDatasource& datasource_; 47 | const HeightmapDatasource& heightmap_; 48 | 49 | public: 50 | GPXLayer(const Projection projection, const GPXDatasource& datasource, const HeightmapDatasource& heightmap); 51 | virtual ~GPXLayer(); 52 | 53 | void Render(const Viewer& viewer); 54 | virtual Tile* SpawnTile(const BBoxi& bbox, int flags) const; 55 | }; 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /libglosm-client/glosm/GPXTile.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef GPXTILE_HH 22 | #define GPXTILE_HH 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | #include 29 | #include 30 | 31 | template 32 | class VertexBuffer; 33 | 34 | class Projection; 35 | class GPXDatasource; 36 | class HeightmapDatasource; 37 | 38 | /** 39 | * A tile of GPX points 40 | * 41 | * This tile type is used by GPXLayer 42 | */ 43 | class GPXTile : public Tile, private NonCopyable { 44 | protected: 45 | std::auto_ptr > points_; 46 | 47 | size_t size_; 48 | 49 | public: 50 | /** 51 | * Constructs tile from vector of points 52 | * 53 | * @param projection projection used to convert fixed-point geometry 54 | * @param points source points 55 | * @param ref reference point of this tile 56 | * @param bbox bounding box of this tile 57 | */ 58 | GPXTile(const Projection& projection, const GPXDatasource& datasource, const HeightmapDatasource& heightmap, const Vector2i& ref, const BBoxi& bbox); 59 | 60 | /** 61 | * Destructor 62 | */ 63 | virtual ~GPXTile(); 64 | 65 | /** 66 | * Render this tile 67 | */ 68 | virtual void Render(); 69 | 70 | /** 71 | * Returns tile size in bytes 72 | */ 73 | virtual size_t GetSize() const; 74 | }; 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /libglosm-client/glosm/GeometryLayer.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef GEOMETRYLAYER_HH 22 | #define GEOMETRYLAYER_HH 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | class Viewer; 30 | class GeometryDatasource; 31 | 32 | /** 33 | * Layer with 3D OpenStreetMap data. 34 | */ 35 | class GeometryLayer : public Layer, public TileManager, private NonCopyable { 36 | protected: 37 | const Projection projection_; 38 | const GeometryDatasource& datasource_; 39 | 40 | public: 41 | GeometryLayer(const Projection projection, const GeometryDatasource& datasource); 42 | virtual ~GeometryLayer(); 43 | 44 | void Render(const Viewer& viewer); 45 | virtual Tile* SpawnTile(const BBoxi& bbox, int flags) const; 46 | }; 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /libglosm-client/glosm/GeometryTile.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef GEOMETRYTILE_HH 22 | #define GEOMETRYTILE_HH 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | #include 29 | 30 | #include 31 | #include 32 | 33 | template 34 | class VertexBuffer; 35 | 36 | class Projection; 37 | class Geometry; 38 | 39 | /** 40 | * A tile of renderable geometry 41 | * 42 | * This tile type is used in GeometryLayer 43 | */ 44 | class GeometryTile : public Tile, private NonCopyable { 45 | protected: 46 | struct Vertex { 47 | Vector3f pos; 48 | Vector3f norm; 49 | 50 | Vertex(const Vector3f& p): pos(p) { 51 | } 52 | }; 53 | 54 | protected: 55 | std::auto_ptr > lines_vertices_; 56 | std::auto_ptr > lines_indices_; 57 | 58 | std::auto_ptr > convex_vertices_; 59 | std::auto_ptr > convex_indices_; 60 | 61 | size_t size_; 62 | 63 | protected: 64 | void CalcFanNormal(Vertex* vertices, int count); 65 | 66 | public: 67 | /** 68 | * Constructs tile from given geometry 69 | * 70 | * @param projection projection used to convert fixed-point geometry 71 | * @param geometry source geometry 72 | * @param ref reference point of this tile 73 | * @param bbox bounding box of this tile 74 | */ 75 | GeometryTile(const Projection& projection, const Geometry& geometry, const Vector2i& ref, const BBoxi& bbox); 76 | 77 | /** 78 | * Destructor 79 | */ 80 | virtual ~GeometryTile(); 81 | 82 | /** 83 | * Render this tile 84 | */ 85 | virtual void Render(); 86 | 87 | /** 88 | * Returns tile size in bytes 89 | */ 90 | virtual size_t GetSize() const; 91 | }; 92 | 93 | #endif 94 | -------------------------------------------------------------------------------- /libglosm-client/glosm/Layer.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef LAYER_HH 22 | #define LAYER_HH 23 | 24 | /** 25 | * Abstact base class for all layers. 26 | * 27 | * Layer is a renderable representation of a single kind of geodata. 28 | */ 29 | class Layer { 30 | public: 31 | virtual ~Layer() { 32 | } 33 | }; 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /libglosm-client/glosm/MercatorProjection.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef MERCATORPROJECTION_HH 22 | #define MERCATORPROJECTION_HH 23 | 24 | #include 25 | 26 | /** 27 | * Mercator/EPSG:3857 projection 28 | * 29 | * This represents Spherical Mercator projection common to OpenStreetMap. 30 | */ 31 | class MercatorProjection : public Projection { 32 | protected: 33 | static Vector3f ProjectImpl(const Vector3i& point, const Vector3i& ref); 34 | static Vector3i UnProjectImpl(const Vector3f& point, const Vector3i& ref); 35 | 36 | public: 37 | MercatorProjection(); 38 | }; 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /libglosm-client/glosm/OrthoViewer.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef ORTHOVIEWER_HH 22 | #define ORTHOVIEWER_HH 23 | 24 | #include 25 | #include 26 | 27 | /** 28 | * Viewer describing orthographic projection. 29 | * 30 | * This viewer is useful for tile rendering. 31 | */ 32 | class OrthoViewer : public Viewer { 33 | public: 34 | OrthoViewer(); 35 | 36 | virtual void SetupViewerMatrix(const Projection& projection) const; 37 | virtual Vector3i GetPos(const Projection& projection) const; 38 | 39 | /** 40 | * Sets bounding box of a viewport in global coordinades 41 | */ 42 | void SetBBox(const BBoxi& pos); 43 | 44 | /** 45 | * Sets skew for pseudo-3D effect. 46 | * 47 | * To retain tile proportions, we can't do proper 3D by 48 | * changing viewing angle, but we can 'skew' objects that 49 | * have height so their sides appear on a top-down view. 50 | * Skew factor may be described as a tangent of desired 51 | * viewing angle, 1.0 is equal to 45 degrees. 52 | * 53 | * @param skew skew ratio 54 | */ 55 | void SetSkew(float skew); 56 | 57 | protected: 58 | BBoxi bbox_; 59 | float skew_; 60 | }; 61 | 62 | #endif 63 | -------------------------------------------------------------------------------- /libglosm-client/glosm/Projection.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef PROJECTION_HH 22 | #define PROJECTION_HH 23 | 24 | #include 25 | 26 | #include 27 | 28 | /** 29 | * Abstract base class for all projections. 30 | * 31 | * Provides a way to translate global fixed-point geometry coordinates 32 | * into relative floating point ones and vice versa. 33 | * 34 | * Since OpenGL works with float data, and float has not enough 35 | * precision to represent geographical coordinates for OpenStreetMap 36 | * objects (up to 1 meter error on high longitudes), we have to use 37 | * fixed point format for data interchange and relative floating-point 38 | * format for rendering. 39 | * 40 | * @note: this class is designed in a way that its derivatives can 41 | * be safely cast to Projection copied and passed by value. 42 | */ 43 | class Projection { 44 | private: 45 | typedef Vector3f(*ProjectFunction)(const Vector3i&, const Vector3i&); 46 | typedef Vector3i(*UnProjectFunction)(const Vector3f&, const Vector3i&); 47 | 48 | private: 49 | ProjectFunction project_; 50 | UnProjectFunction unproject_; 51 | 52 | protected: 53 | Projection(ProjectFunction pf, UnProjectFunction uf); 54 | 55 | public: 56 | /** 57 | * Translates a point from global fixed-point to relative 58 | * floating-point coordinate system. 59 | * 60 | * @param point point to translate 61 | * @param ref reference point which denotes the center of 62 | * local coordinate system 63 | * @return translated point 64 | */ 65 | Vector3f Project(const Vector3i& point, const Vector3i& ref) const; 66 | 67 | /** 68 | * Translates a point from relative floating-point to global 69 | * fixed-point coordinate system. 70 | * 71 | * @param point point to translate 72 | * @param ref reference point which denotes the center of 73 | * local coordinate system 74 | * @return translated point 75 | */ 76 | Vector3i UnProject(const Vector3f& point, const Vector3i& ref) const; 77 | 78 | /** 79 | * Translates bunch of points from relative floating-point 80 | * to global fixed-point coordinate system. 81 | * 82 | * @param in reference to input vector of points 83 | * @param out reference to output vector of translated 84 | * points (which is appended) 85 | * @param ref reference point which denotes the center of 86 | * local coordinate system 87 | */ 88 | void ProjectPoints(const std::vector& in, const Vector3i& ref, std::vector& out) const; 89 | }; 90 | 91 | #endif 92 | -------------------------------------------------------------------------------- /libglosm-client/glosm/Renderable.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef RENDERABLE_HH 22 | #define RENDERABLE_HH 23 | 24 | /** 25 | * An abstract renderable object 26 | */ 27 | class Renderable { 28 | public: 29 | virtual ~Renderable() { 30 | } 31 | 32 | virtual void Render() = 0; 33 | }; 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /libglosm-client/glosm/SphericalProjection.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef SPHERICALPROJECTION_HH 22 | #define SPHERICALPROJECTION_HH 23 | 24 | #include 25 | 26 | /** 27 | * Spherical projection 28 | * 29 | * This represents Spherical projection. 30 | */ 31 | class SphericalProjection : public Projection { 32 | protected: 33 | static Vector3f ProjectImpl(const Vector3i& point, const Vector3i& ref); 34 | static Vector3i UnProjectImpl(const Vector3f& point, const Vector3i& ref); 35 | 36 | public: 37 | SphericalProjection(); 38 | }; 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /libglosm-client/glosm/TerrainLayer.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef TERRAINLAYER_HH 22 | #define TERRAINLAYER_HH 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | class Viewer; 30 | class HeightmapDatasource; 31 | 32 | /** 33 | * Layer with 3D terrain data. 34 | */ 35 | class TerrainLayer : public Layer, public TileManager, private NonCopyable { 36 | protected: 37 | const Projection projection_; 38 | HeightmapDatasource& datasource_; 39 | 40 | public: 41 | TerrainLayer(const Projection projection, HeightmapDatasource& datasource); 42 | virtual ~TerrainLayer(); 43 | 44 | void Render(const Viewer& viewer); 45 | virtual Tile* SpawnTile(const BBoxi& bbox, int flags) const; 46 | }; 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /libglosm-client/glosm/TerrainTile.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef TERRAINTILE_HH 22 | #define TERRAINTILE_HH 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | #include 29 | 30 | #include 31 | #include 32 | 33 | class SimpleVertexBuffer; 34 | class HeightmapDatasource; 35 | 36 | class Projection; 37 | 38 | template 39 | class VertexBuffer; 40 | 41 | /** 42 | * A terrain tile 43 | * 44 | * This tile type is used by TerrainLayer 45 | */ 46 | class TerrainTile : public Tile, private NonCopyable { 47 | protected: 48 | struct TerrainVertex { 49 | Vector3f pos; 50 | Vector3f norm; 51 | }; 52 | 53 | protected: 54 | std::auto_ptr > vbo_; 55 | std::auto_ptr > ibo_; 56 | 57 | size_t size_; 58 | 59 | public: 60 | TerrainTile(const Projection& projection, HeightmapDatasource& datasource, const Vector2i& ref, const BBoxi& bbox); 61 | 62 | /** 63 | * Destructor 64 | */ 65 | virtual ~TerrainTile(); 66 | 67 | /** 68 | * Render this tile 69 | */ 70 | virtual void Render(); 71 | 72 | /** 73 | * Returns tile size in bytes 74 | */ 75 | virtual size_t GetSize() const; 76 | }; 77 | 78 | #endif 79 | -------------------------------------------------------------------------------- /libglosm-client/glosm/Tile.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef TILE_HH 22 | #define TILE_HH 23 | 24 | #include 25 | 26 | #include /* for size_t */ 27 | 28 | /** 29 | * Abstract class for all geodata tiles. 30 | * 31 | * Tile is just an abstract hunk of geodata ready for rendering. 32 | * Each tile has center stored in global fixed-point coords, while 33 | * all the actual data is stored in fixed-point format relative to 34 | * that center. As the tiles are expected to be small enough, this 35 | * prevents floating point errors to be noticeable. 36 | */ 37 | class Tile { 38 | protected: 39 | /** 40 | * Global coordinates of tile center. 41 | */ 42 | const Vector2i reference_; 43 | 44 | public: 45 | /** 46 | * Constructs tile 47 | */ 48 | Tile(const Vector2i& ref) : reference_(ref) {} 49 | 50 | /** 51 | * Destructor 52 | */ 53 | virtual ~Tile() {} 54 | 55 | /** 56 | * Renders tile 57 | */ 58 | virtual void Render() = 0; 59 | 60 | /** 61 | * Returns tile size in bytes 62 | */ 63 | virtual size_t GetSize() const = 0; 64 | 65 | /** 66 | * Returns tile reference point 67 | */ 68 | const Vector2i& GetReference() const { 69 | return reference_; 70 | } 71 | }; 72 | 73 | #endif 74 | -------------------------------------------------------------------------------- /libglosm-client/glosm/VertexBuffer.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef VERTEXBUFFER_HH 22 | #define VERTEXBUFFER_HH 23 | 24 | #include 25 | #include 26 | 27 | #include 28 | 29 | #include 30 | 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | #define BUFFER_OFFSET(i) ((char *)NULL + (i)) 37 | 38 | /** 39 | * Vertex Buffer Object 40 | * 41 | * This class is mainly a wrapper around OpenGL VBO's. However, 42 | * since delayed VBO creation is common in glosm, this may also 43 | * be used as a vector of vertex data, which turns into VBO on 44 | * first Bind() or Freeze() call. 45 | */ 46 | template 47 | class VertexBuffer : private NonCopyable { 48 | public: 49 | typedef std::vector DataVector; 50 | 51 | protected: 52 | GLuint buffer_id_; 53 | size_t size_; 54 | GLenum type_; 55 | GLenum mode_; 56 | std::auto_ptr ram_data_; 57 | 58 | protected: 59 | void CreateBufferObject(const T* data, int count) { 60 | glGenBuffers(1, &buffer_id_); 61 | glBindBuffer(type_, buffer_id_); 62 | glBufferData(type_, count * sizeof(T), data, mode_); 63 | glBindBuffer(type_, 0); 64 | size_ = count; 65 | } 66 | 67 | public: 68 | /** 69 | * Constructs dynamic vertex buffer 70 | * 71 | * Vertex buffer constructed this way is ready to be filled 72 | * with data and be generally used as vector of T's via 73 | * Data() call. OpenGL buffers are not created until first 74 | * Bind() or Freeze() calls 75 | * 76 | * @param type buffer type (GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER) 77 | */ 78 | VertexBuffer(GLenum type = GL_ARRAY_BUFFER, GLenum mode = GL_STATIC_DRAW): buffer_id_(0), size_(0), type_(type), mode_(mode), ram_data_(new DataVector) { 79 | } 80 | 81 | /** 82 | * Constructs static vertex buffer 83 | * 84 | * Constructs OpenGL VBO directly from preexisting data. 85 | * 86 | * @param type buffer type (GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER) 87 | * @param data pointer to array of data items 88 | * @param count number of items 89 | */ 90 | VertexBuffer(GLenum type, const T* data, int count): type_(type) { 91 | CreateBufferObject(data, count); 92 | } 93 | 94 | /** 95 | * Destructor 96 | */ 97 | ~VertexBuffer() { 98 | if (buffer_id_) 99 | glDeleteBuffers(1, &buffer_id_); 100 | } 101 | 102 | /** 103 | * Gives access to vector of vertex data 104 | * 105 | * @return reference to data vector 106 | */ 107 | DataVector& Data() { 108 | assert(ram_data_.get()); 109 | return *ram_data_; 110 | } 111 | 112 | /** 113 | * Forcibly converts data into OpenGL VBO 114 | * 115 | * @return true if VBO was created 116 | */ 117 | bool Freeze() { 118 | if (ram_data_.get()) { 119 | CreateBufferObject(ram_data_->data(), ram_data_->size()); 120 | ram_data_.reset(NULL); 121 | return true; 122 | } 123 | return false; 124 | } 125 | 126 | /** 127 | * Binds OpenGL buffer 128 | * 129 | * Creates it from vertex data if necessary 130 | */ 131 | void Bind() { 132 | Freeze(); 133 | 134 | assert(buffer_id_); 135 | glBindBuffer(type_, buffer_id_); 136 | } 137 | 138 | /** 139 | * Unbinds OpenGL buffer 140 | */ 141 | void UnBind() { 142 | glBindBuffer(type_, 0); 143 | } 144 | 145 | /** 146 | * Returns number of items in VBO 147 | */ 148 | size_t GetSize() const { 149 | if (ram_data_.get()) 150 | return ram_data_->size(); 151 | return size_; 152 | } 153 | 154 | /** 155 | * Returns size of VBO in bytes 156 | */ 157 | size_t GetFootprint() const { 158 | if (ram_data_.get()) 159 | return sizeof(T) * ram_data_->size(); 160 | return sizeof(T) * size_; 161 | } 162 | }; 163 | 164 | #endif 165 | -------------------------------------------------------------------------------- /libglosm-client/glosm/Viewer.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef VIEWER_HH 22 | #define VIEWER_HH 23 | 24 | #include 25 | 26 | class Projection; 27 | 28 | /** 29 | * Abstract base class for all viewers. 30 | * 31 | * Viewer is a way to represent which part of map we need to render. 32 | * For example, for first-person viewer that may be eye's position 33 | * and look direction. This class is used in rendering process to 34 | * properly setup projection matrix and in layers to determine which 35 | * objects are close enough to eye to be loaded and displayed. 36 | */ 37 | class Viewer { 38 | public: 39 | /** 40 | * Setups OpenGL projection matrix for the viewer 41 | * 42 | * @param projection projection used in this world 43 | */ 44 | virtual void SetupViewerMatrix(const Projection& projection) const = 0; 45 | 46 | /** 47 | * Returns pseudo-position of a viewer. 48 | * 49 | * @param projection projection used in the world 50 | * @return pseudo-position of a viewer 51 | */ 52 | virtual Vector3i GetPos(const Projection& projection) const = 0; 53 | }; 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /libglosm-client/glosm/util/gl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef UTIL_GL_HH 22 | #define UTIL_GL_HH 23 | 24 | #if defined(WITH_GLEW) 25 | # include 26 | #else 27 | # define GL_GLEXT_PROTOTYPES 28 | #endif 29 | 30 | #if defined(WITH_GLES) 31 | # include 32 | # include 33 | #elif defined(WITH_GLES2) 34 | # include 35 | # include 36 | #elif defined(__APPLE__) 37 | # include 38 | # include 39 | #else 40 | # include 41 | # include 42 | #endif 43 | 44 | /*class OpenGLError : public Exception() { 45 | static void Check() { 46 | GLenum e; 47 | if ((e = glGetError()) == GL_NO_ERROR) 48 | return; 49 | 50 | OpenGLError e; 51 | 52 | while (glGetError) { 53 | 54 | } 55 | } 56 | };*/ 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /libglosm-client/mglu.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef MGLU_H 22 | #define MGLU_H 23 | 24 | #include "mglu.h" 25 | 26 | #include 27 | 28 | #include 29 | 30 | void mglFrustum(float left, float right, float bottom, float top, float znear, float zfar) { 31 | float matrix[16] = { 32 | 2.0f * znear / (right - left), 0.0f, 0.0f, 0.0f, 33 | 0.0f, 2.0f * znear / (top - bottom), 0.0f, 0.0f, 34 | (right + left) / (right - left), (top + bottom) / (top - bottom), - (zfar - znear) / (zfar - znear), -1.0f, 35 | 0.0f, 0.0f, -2.0f * znear * zfar / (zfar - znear), 0.0f, 36 | }; 37 | 38 | glLoadMatrixf(matrix); 39 | } 40 | 41 | void mgluPerspective(float fovy, float aspect, float znear, float zfar) { 42 | float xmin, xmax, ymin, ymax; 43 | 44 | ymax = znear * tan(fovy * M_PI / 360.0f); 45 | ymin = -ymax; 46 | xmax = ymax * aspect; 47 | xmin = -xmax; 48 | 49 | mglFrustum(xmin, xmax, ymin, ymax, znear, zfar); 50 | } 51 | 52 | void mgluLookAt(float eyex, float eyey, float eyez, float centerx, float centery, float centerz, float upx, float upy, float upz) { 53 | Vector3f forward = (Vector3f(centerx, centery, centerz) - Vector3f(eyex, eyey, eyez)).Normalized(); 54 | Vector3f side = forward.CrossProduct(Vector3f(upx, upy, upz)).Normalized(); 55 | Vector3f up = side.CrossProduct(forward); 56 | 57 | float matrix[16] = { 58 | side.x, up.x, -forward.x, 0.0f, 59 | side.y, up.y, -forward.y, 0.0f, 60 | side.z, up.z, -forward.z, 0.0f, 61 | 0.0f, 0.0f, 0.0f, 1.0f, 62 | }; 63 | 64 | glLoadMatrixf(matrix); 65 | glTranslatef(-eyex, -eyey, -eyez); 66 | } 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /libglosm-client/mglu.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef MGLU_H 22 | #define MGLU_H 23 | 24 | /* Implementations of some libGLU functions */ 25 | 26 | void mglFrustum(float left, float right, float bottom, float top, float znear, float zfar); 27 | void mgluPerspective(float fovy, float aspect, float znear, float zfar); 28 | void mgluLookAt(float eyex, float eyey, float eyez, float centerx, float centery, float centerz, float upx, float upy, float upz); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /libglosm-geomgen/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Targets 2 | SET(SOURCES 3 | GeometryGenerator.cc 4 | MetricBasis.cc 5 | ) 6 | 7 | SET(HEADERS 8 | glosm/GeometryGenerator.hh 9 | glosm/MetricBasis.hh 10 | ) 11 | 12 | INCLUDE_DIRECTORIES(. ../libglosm-server) 13 | 14 | ADD_LIBRARY(glosm-geomgen SHARED ${SOURCES}) 15 | TARGET_LINK_LIBRARIES(glosm-geomgen glosm-server) 16 | 17 | # Installation 18 | # SET_TARGET_PROPERTIES(glosm-geomgen PROPERTIES SOVERSION 1) 19 | INSTALL(FILES ${HEADERS} DESTINATION include/glosm) 20 | 21 | IF(NOT CMAKE_CROSSCOMPILING) 22 | INSTALL(TARGETS glosm-geomgen LIBRARY DESTINATION ${LIBDIR}) 23 | ENDIF(NOT CMAKE_CROSSCOMPILING) 24 | -------------------------------------------------------------------------------- /libglosm-geomgen/MetricBasis.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #include 22 | #include 23 | 24 | MetricBasis::MetricBasis(const Vector3i ref, const Vector3d& x, const Vector3d& y, const Vector3d& z): 25 | ref_(ref), 26 | x_(x), 27 | y_(y), 28 | z_(z) { 29 | } 30 | 31 | MetricBasis::MetricBasis(const Vector3i ref, const Vector3d& x): 32 | ref_(ref), 33 | x_(x), 34 | y_(-x.CrossProduct(Vector3d(0.0, 0.0, 1.0))), 35 | z_(Vector3d(0.0, 0.0, 1.0)) { 36 | } 37 | 38 | Vector3i MetricBasis::Get(double x, double y, double z) { 39 | return FromLocalMetric(x_ * x + y_ * y + z_ * z, ref_); 40 | } 41 | -------------------------------------------------------------------------------- /libglosm-geomgen/glosm/GeometryGenerator.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef GEOMETRYGENERATOR_HH 22 | #define GEOMETRYGENERATOR_HH 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | class OsmDatasource; 29 | class HeightmapDatasource; 30 | class Geometry; 31 | 32 | class GeometryGenerator : public GeometryDatasource { 33 | protected: 34 | const OsmDatasource& datasource_; 35 | HeightmapDatasource& heightmap_ds_; 36 | 37 | public: 38 | GeometryGenerator(const OsmDatasource& datasource, HeightmapDatasource& heightmapds); 39 | void GetGeometry(Geometry& geometry, const BBoxi& bbox, int flags = 0) const; 40 | 41 | virtual Vector2i GetCenter() const; 42 | virtual BBoxi GetBBox() const; 43 | }; 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /libglosm-geomgen/glosm/MetricBasis.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef METRICBASIS_HH 22 | #define METRICBASIS_HH 23 | 24 | #include 25 | 26 | /** 27 | * Convenient metric basis class for geometry construction 28 | */ 29 | class MetricBasis { 30 | public: 31 | /** 32 | * Creates basis out of 3 axis vectors (expected to be normalized) 33 | */ 34 | MetricBasis(const Vector3i ref, const Vector3d& x, const Vector3d& y, const Vector3d& z); 35 | 36 | /** 37 | * Creates basis out of a single axis vectors (expected to be normalized) 38 | * 39 | * Z axis is considered to be [0, 0, 1] and Y is calculated as 40 | * perpendicular to XZ plane 41 | */ 42 | MetricBasis(const Vector3i ref, const Vector3d& x); 43 | 44 | /** 45 | * Returns basis-relative point as global fixed-point coordinates 46 | */ 47 | Vector3i Get(double x, double y, double z); 48 | 49 | protected: 50 | Vector3i ref_; 51 | Vector3d x_, y_, z_; 52 | }; 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /libglosm-server/BBox.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #include 22 | 23 | #include 24 | 25 | #include 26 | 27 | template<> 28 | BBox BBox::ForEarth() { 29 | return BBox(-1800000000, -900000000, 1800000000, 900000000); 30 | } 31 | 32 | template<> 33 | BBox BBox::ForMercatorTile(int zoom, int x, int y) { 34 | double zoommult = 1.0 / (double)(1 << zoom); 35 | 36 | return BBox( 37 | (osmint_t)round(((double)x) * zoommult * 3600000000.0 - 1800000000.0), 38 | (osmint_t)round(-unmercator(((float)y + 1.0) * zoommult * M_PI * 2.0 - M_PI) / M_PI * 1800000000.0), 39 | (osmint_t)round(((double)x + 1.0) * zoommult * 3600000000.0 - 1800000000.0), 40 | (osmint_t)round(-unmercator(((float)y) * zoommult * M_PI * 2.0 - M_PI) / M_PI * 1800000000.0) 41 | ); 42 | } 43 | 44 | template<> 45 | BBox BBox::ForGeoTile(int zoom, int x, int y) { 46 | double zoommult = 1.0 / (double)(1 << zoom); 47 | 48 | return BBox( 49 | (osmint_t)round( 50 | (double)x * zoommult * 3600000000.0 - 1800000000.0 51 | ), 52 | (osmint_t)round( 53 | -((double)y + 1.0) * zoommult * 1800000000.0 + 900000000.0 54 | ), 55 | (osmint_t)round( 56 | ((double)x + 1.0) * zoommult * 3600000000.0 - 1800000000.0 57 | ), 58 | (osmint_t)round( 59 | -(double)y * zoommult * 1800000000.0 + 900000000.0 60 | ) 61 | ); 62 | } 63 | -------------------------------------------------------------------------------- /libglosm-server/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Depends 2 | FIND_PACKAGE(EXPAT REQUIRED) 3 | FIND_PACKAGE(Threads REQUIRED) 4 | 5 | # Type size checks 6 | INCLUDE(CheckTypeSize) 7 | 8 | SET(CMAKE_EXTRA_INCLUDE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/glosm/osmtypes.h") 9 | CHECK_TYPE_SIZE("osmid_t" OSMID_T_SIZE) 10 | CHECK_TYPE_SIZE("osmint_t" OSMINT_T_SIZE) 11 | CHECK_TYPE_SIZE("osmlong_t" OSMLONG_T_SIZE) 12 | CHECK_TYPE_SIZE("float" FLOAT_SIZE) 13 | SET(CMAKE_EXTRA_INCLUDE_FILES) 14 | 15 | IF (${OSMID_T_SIZE} LESS 8) 16 | MESSAGE(FATAL_ERROR "osmid_t size too small (likely easily fixable, see osmtypes.h)") 17 | ENDIF(${OSMID_T_SIZE} LESS 8) 18 | IF (${OSMINT_T_SIZE} LESS 4) 19 | MESSAGE(FATAL_ERROR "osmint_t size too small (likely easily fixable, see osmtypes.h)") 20 | ENDIF(${OSMINT_T_SIZE} LESS 4) 21 | IF (${OSMLONG_T_SIZE} LESS 8) 22 | MESSAGE(FATAL_ERROR "osmlong_t size too small (likely easily fixable, see osmtypes.h)") 23 | ENDIF(${OSMLONG_T_SIZE} LESS 8) 24 | 25 | # Options 26 | OPTION(TRUSTED_XML "Disable XML sanity checking for faster parsing" OFF) 27 | 28 | IF(TRUSTED_XML) 29 | ADD_DEFINITIONS(-DTRUSTED_XML) 30 | ENDIF(TRUSTED_XML) 31 | 32 | IF(WITH_GLES) 33 | ADD_DEFINITIONS(-DWITH_GLES) 34 | ENDIF(WITH_GLES) 35 | 36 | # Targets 37 | SET(SOURCES 38 | BBox.cc 39 | DummyHeightmap.cc 40 | Exception.cc 41 | Geometry.cc 42 | GeometryOperations.cc 43 | Guard.cc 44 | ParsingHelpers.cc 45 | PreloadedGPXDatasource.cc 46 | PreloadedXmlDatasource.cc 47 | SRTMDatasource.cc 48 | Timer.cc 49 | WayMerger.cc 50 | XMLParser.cc 51 | ) 52 | 53 | SET(HEADERS 54 | glosm/BBox.hh 55 | glosm/DummyHeightmap.hh 56 | glosm/Exception.hh 57 | glosm/geomath.h 58 | glosm/Geometry.hh 59 | glosm/GeometryDatasource.hh 60 | glosm/GeometryOperations.hh 61 | glosm/GPXDatasource.hh 62 | glosm/Guard.hh 63 | glosm/HeightmapDatasource.hh 64 | glosm/id_map.hh 65 | glosm/Math.hh 66 | glosm/Misc.hh 67 | glosm/NonCopyable.hh 68 | glosm/OsmDatasource.hh 69 | glosm/osmtypes.h 70 | glosm/ParsingHelpers.hh 71 | glosm/PreloadedGPXDatasource.hh 72 | glosm/PreloadedXmlDatasource.hh 73 | glosm/SRTMDatasource.hh 74 | glosm/Timer.hh 75 | glosm/WayMerger.hh 76 | glosm/XMLParser.hh 77 | ) 78 | 79 | INCLUDE_DIRECTORIES(. ${EXPAT_INCLUDE_DIR}) 80 | 81 | ADD_LIBRARY(glosm-server SHARED ${SOURCES}) 82 | TARGET_LINK_LIBRARIES(glosm-server ${EXPAT_LIBRARY} ${CMAKE_THREAD_LIBS_INIT}) 83 | 84 | # Installation 85 | # SET_TARGET_PROPERTIES(glosm-server PROPERTIES SOVERSION 1) 86 | INSTALL(FILES ${HEADERS} DESTINATION include/glosm) 87 | 88 | IF(NOT CMAKE_CROSSCOMPILING) 89 | INSTALL(TARGETS glosm-server LIBRARY DESTINATION ${LIBDIR}) 90 | ENDIF(NOT CMAKE_CROSSCOMPILING) 91 | -------------------------------------------------------------------------------- /libglosm-server/DummyHeightmap.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #include 22 | 23 | DummyHeightmap::DummyHeightmap(osmint_t height) : height_(height) { 24 | } 25 | 26 | DummyHeightmap::~DummyHeightmap() { 27 | } 28 | 29 | void DummyHeightmap::GetHeightmap(const BBoxi& bbox, int extramargin, Heightmap& out) const { 30 | out.width = 2 + extramargin * 2; 31 | out.height = 2 + extramargin * 2; 32 | 33 | out.bbox = BBoxi( 34 | bbox.left - (bbox.right - bbox.left) * extramargin, 35 | bbox.bottom - (bbox.top - bbox.bottom) * extramargin, 36 | bbox.right + (bbox.right - bbox.left) * extramargin, 37 | bbox.top + (bbox.top - bbox.bottom) * extramargin 38 | ); 39 | 40 | out.points.resize(out.width * out.height); 41 | 42 | for (Heightmap::HeightmapPoints::iterator i = out.points.begin(); i != out.points.end(); ++i) 43 | *i = height_; 44 | } 45 | 46 | osmint_t DummyHeightmap::GetHeight(const Vector2i& /*unused*/) const { 47 | return height_; 48 | } 49 | -------------------------------------------------------------------------------- /libglosm-server/Exception.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #include 22 | 23 | #include 24 | #include 25 | 26 | namespace Private { 27 | void SafeStringBuffer::EnsureSize(unsigned int size) { 28 | unsigned int newallocated_ = allocated_; 29 | while (size > newallocated_) 30 | newallocated_ *= 2; 31 | 32 | if (newallocated_ > allocated_) { 33 | char* newbuffer = new char[newallocated_]; 34 | memcpy(newbuffer, buffer_, used_); 35 | delete[] buffer_; 36 | buffer_ = newbuffer; 37 | allocated_ = newallocated_; 38 | } 39 | } 40 | 41 | SafeStringBuffer::SafeStringBuffer(unsigned int reserve) 42 | : reserve_(reserve), 43 | allocated_(initial_size_ + reserve), 44 | used_(0), 45 | buffer_(new char[allocated_]) { 46 | } 47 | 48 | SafeStringBuffer::SafeStringBuffer(const char* message) 49 | : reserve_(0), 50 | allocated_(strlen(message) + 1), 51 | used_(allocated_ - 1), 52 | buffer_(new char[allocated_]) { 53 | strcpy(buffer_, message); 54 | } 55 | 56 | SafeStringBuffer::SafeStringBuffer(const SafeStringBuffer& other) 57 | : std::streambuf(), 58 | reserve_(other.reserve_), 59 | allocated_(other.allocated_), 60 | used_(other.used_), 61 | buffer_( new char[allocated_]) { 62 | memcpy(buffer_, other.buffer_, used_); 63 | } 64 | 65 | SafeStringBuffer::~SafeStringBuffer() { 66 | delete[] buffer_; 67 | } 68 | 69 | void SafeStringBuffer::SetReserve(unsigned int reserve) { 70 | EnsureSize(used_ + reserve + 1); 71 | reserve_ = reserve; 72 | } 73 | 74 | std::streamsize SafeStringBuffer::xsputn(const char* s, std::streamsize n) { 75 | EnsureSize(used_ + n + reserve_ + 1); 76 | memcpy(buffer_ + used_, s, n); 77 | used_ += n; 78 | return n; 79 | } 80 | 81 | std::streamsize SafeStringBuffer::AppendReserve(const char* s, std::streamsize n) throw() { 82 | if ((unsigned int)n > reserve_) 83 | n = reserve_; 84 | 85 | memcpy(buffer_ + used_, s, n); 86 | used_ += n; 87 | reserve_ -= n; 88 | 89 | return n; 90 | } 91 | 92 | const char* SafeStringBuffer::c_str() throw() { 93 | buffer_[used_] = '\0'; 94 | return buffer_; 95 | } 96 | 97 | ExceptionBase::ExceptionBase() { 98 | } 99 | 100 | ExceptionBase::ExceptionBase(const ExceptionBase& e): std::exception(), message_(e.what()/*message_*/) { 101 | } 102 | 103 | ExceptionBase::~ExceptionBase() throw() { 104 | } 105 | 106 | const char* ExceptionBase::what() const throw() { 107 | return message_.c_str(); 108 | } 109 | }; 110 | 111 | SystemError::SystemError() : errno_(errno) { 112 | message_.SetReserve(strlen(strerror(errno_)) + 3); 113 | } 114 | 115 | SystemError::SystemError(int errn) : errno_(errn) { 116 | message_.SetReserve(strlen(strerror(errno_)) + 3); 117 | } 118 | 119 | SystemError::SystemError(const SystemError& e): Exception(e), errno_(e.errno_) { 120 | } 121 | 122 | SystemError::~SystemError() throw() { 123 | } 124 | 125 | const char* SystemError::what() const throw() { 126 | message_.AppendReserve(" (", 2); 127 | message_.AppendReserve(strerror(errno), strlen(strerror(errno))); 128 | message_.AppendReserve(")", 1); 129 | return message_.c_str(); 130 | } 131 | -------------------------------------------------------------------------------- /libglosm-server/Guard.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #include 22 | 23 | Guard::Guard(pthread_mutex_t& mutex): mutex_(mutex) { 24 | pthread_mutex_lock(&mutex_); 25 | } 26 | 27 | Guard::~Guard() { 28 | pthread_mutex_unlock(&mutex_); 29 | } 30 | -------------------------------------------------------------------------------- /libglosm-server/ParsingHelpers.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #include 22 | 23 | int ParseCoord(const char* str) { 24 | return ParseInt<7>(str); 25 | } 26 | 27 | int ParseEle(const char* str) { 28 | return ParseInt<2>(str); 29 | } 30 | 31 | BBoxi ParseBounds(const char** atts) { 32 | BBoxi bbox(BBoxi::Empty()); 33 | 34 | for (const char** att = atts; *att; ++att) { 35 | if (StrEq<-1>(*att, "minlat")) 36 | bbox.bottom = ParseCoord(*(++att)); 37 | else if (StrEq<-1>(*att, "maxlat")) 38 | bbox.top = ParseCoord(*(++att)); 39 | else if (StrEq<-1>(*att, "minlon")) 40 | bbox.left = ParseCoord(*(++att)); 41 | else if (StrEq<-1>(*att, "maxlon")) 42 | bbox.right = ParseCoord(*(++att)); 43 | else 44 | ++att; 45 | } 46 | 47 | if (bbox.IsEmpty()) 48 | throw ParsingException() << "incorrect bounding box"; 49 | 50 | return bbox; 51 | } 52 | 53 | BBoxi ParseBound(const char** atts) { 54 | BBoxi bbox(BBoxi::Empty()); 55 | 56 | for (const char** att = atts; *att; ++att) { 57 | if (StrEq<-1>(*att, "box")) { 58 | std::string s(*(++att)); 59 | /* comma positions */ 60 | size_t cpos1, cpos2, cpos3; 61 | if ((cpos1 = s.find(',')) == std::string::npos) 62 | throw ParsingException() << "bad bbox format"; 63 | if ((cpos2 = s.find(',', cpos1+1)) == std::string::npos) 64 | throw ParsingException() << "bad bbox format"; 65 | if ((cpos3 = s.find(',', cpos2+1)) == std::string::npos) 66 | throw ParsingException() << "bad bbox format"; 67 | 68 | bbox.bottom = ParseCoord(s.substr(0, cpos1).c_str()); 69 | bbox.left = ParseCoord(s.substr(cpos1+1, cpos2-cpos1-1).c_str()); 70 | bbox.top = ParseCoord(s.substr(cpos2+1, cpos3-cpos2-1).c_str()); 71 | bbox.right = ParseCoord(s.substr(cpos3+1).c_str()); 72 | } else { 73 | ++att; 74 | } 75 | } 76 | 77 | return bbox; 78 | } 79 | -------------------------------------------------------------------------------- /libglosm-server/PreloadedGPXDatasource.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #include 22 | #include 23 | 24 | PreloadedGPXDatasource::PreloadedGPXDatasource() : XMLParser(XMLParser::HANDLE_ELEMENTS | XMLParser::HANDLE_CHARDATA) { 25 | } 26 | 27 | PreloadedGPXDatasource::~PreloadedGPXDatasource() { 28 | } 29 | 30 | void PreloadedGPXDatasource::StartElement(const char* name, const char** atts) { 31 | if (tag_level_ == 4 && current_tag_ == TRKPT && StrEq<-1>(name, "ele")) { 32 | current_tag_ = ELE; 33 | } else if (tag_level_ == 3 && current_tag_ == TRKSEG && StrEq<-1>(name, "trkpt")) { 34 | current_tag_ = TRKPT; 35 | 36 | int lat = 0; 37 | int lon = 0; 38 | for (const char** att = atts; *att; ++att) { 39 | if (StrEq<2>(*att, "lat")) 40 | lat = ParseCoord(*(++att)); 41 | else if (StrEq<2>(*att, "lon")) 42 | lon = ParseCoord(*(++att)); 43 | else 44 | ++att; 45 | } 46 | 47 | points_.push_back(Vector3i(lon, lat, 0)); 48 | } else if (tag_level_ == 2 && current_tag_ == TRK && StrEq<-1>(name, "trkseg")) { 49 | current_tag_ = TRKSEG; 50 | } else if (tag_level_ == 1 && current_tag_ == GPX && StrEq<-1>(name, "trk")) { 51 | current_tag_ = TRK; 52 | } else if (tag_level_ == 0 && current_tag_ == NONE && StrEq<-1>(name, "gpx")) { 53 | current_tag_ = GPX; 54 | } else if (tag_level_ == 0) { 55 | throw ParsingException() << "unexpected root element (" << name << " instead of gpx)"; 56 | } 57 | 58 | ++tag_level_; 59 | } 60 | 61 | void PreloadedGPXDatasource::EndElement(const char* /*name*/) { 62 | if (tag_level_ == 5 && current_tag_ == ELE) 63 | current_tag_ = TRKPT; 64 | else if (tag_level_ == 4 && current_tag_ == TRKPT) 65 | current_tag_ = TRKSEG; 66 | else if (tag_level_ == 3 && current_tag_ == TRKSEG) 67 | current_tag_ = TRK; 68 | else if (tag_level_ == 2 && current_tag_ == TRK) 69 | current_tag_ = GPX; 70 | else if (tag_level_ == 1 && current_tag_ == GPX) 71 | current_tag_ = NONE; 72 | 73 | --tag_level_; 74 | } 75 | 76 | void PreloadedGPXDatasource::CharacterData(const char* data, int len) { 77 | std::string buf(data, len); 78 | if (tag_level_ == 5 && current_tag_ == ELE) 79 | points_.back().z = ParseEle(buf.c_str()); 80 | } 81 | 82 | void PreloadedGPXDatasource::Load(const char* filename) { 83 | current_tag_ = NONE; 84 | tag_level_ = 0; 85 | 86 | XMLParser::Load(filename); 87 | } 88 | 89 | void PreloadedGPXDatasource::GetPoints(std::vector& out, const BBoxi& bbox) const { 90 | for (PointsVector::const_iterator i = points_.begin(); i != points_.end(); ++i) 91 | if (bbox.Contains(*i)) 92 | out.push_back(*i); 93 | } 94 | -------------------------------------------------------------------------------- /libglosm-server/Timer.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #include 22 | 23 | #include /* for NULL */ 24 | 25 | Timer::Timer() { 26 | Count(); 27 | } 28 | 29 | float Timer::Count() { 30 | struct timeval current; 31 | gettimeofday(¤t, NULL); 32 | 33 | float retval = (float)(current.tv_sec - last_.tv_sec) + (float)(current.tv_usec - last_.tv_usec)/1000000.0f; 34 | 35 | last_ = current; 36 | 37 | return retval; 38 | } 39 | -------------------------------------------------------------------------------- /libglosm-server/WayMerger.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #include 22 | #include 23 | 24 | #include 25 | #include 26 | 27 | WayMerger::WayMerger() { 28 | } 29 | 30 | void WayMerger::AddWay(const OsmDatasource::Way::NodesList& nodes) { 31 | way_by_node.insert(std::make_pair(nodes.front(), &nodes)); 32 | way_by_node_rev.insert(std::make_pair(nodes.back(), &nodes)); 33 | } 34 | 35 | bool WayMerger::GetNextWay(OsmDatasource::Way::NodesList& outnodes) { 36 | OsmDatasource::Way::NodesList tempnodes; 37 | 38 | std::back_insert_iterator inserter(tempnodes); 39 | 40 | const OsmDatasource::Way::NodesList* next_chain; 41 | while (!way_by_node.empty()) { 42 | if (tempnodes.empty()) { 43 | /* no current nodes, add just any chain */ 44 | next_chain = PickChain(); 45 | assert(next_chain); 46 | std::copy(next_chain->begin(), next_chain->end(), inserter); 47 | } else { 48 | /* find next chain, first try front map, then reverse */ 49 | if ((next_chain = PickChain(tempnodes.back())) != NULL) { 50 | std::copy(++next_chain->begin(), next_chain->end(), inserter); 51 | } else if ((next_chain = PickChain(tempnodes.back(), true)) != NULL) { 52 | std::copy(++next_chain->rbegin(), next_chain->rend(), inserter); 53 | } else { 54 | /* no suitable next chain, drop partial way */ 55 | /* XXX: (or, we can return it if we need unclosed ways too */ 56 | tempnodes.clear(); 57 | } 58 | } 59 | 60 | /* check if we have complete cycle */ 61 | if (!tempnodes.empty() && tempnodes.front() == tempnodes.back()) { 62 | outnodes.swap(tempnodes); 63 | return true; 64 | } 65 | } 66 | 67 | return false; 68 | } 69 | 70 | const OsmDatasource::Way::NodesList* WayMerger::PickChain(osmid_t id, bool reverse) { 71 | WayByTipMap& primary = reverse ? way_by_node_rev : way_by_node; 72 | WayByTipMap& secondary = reverse ? way_by_node : way_by_node_rev; 73 | 74 | /* find chain starting with given id in primary map */ 75 | WayByTipMap::iterator primary_iterator = ((id == 0) ? primary.begin() : primary.find(id)); 76 | if (primary_iterator == primary.end()) 77 | return NULL; 78 | 79 | /* save chain poiter which we will return */ 80 | const OsmDatasource::Way::NodesList* ret = primary_iterator->second; 81 | 82 | /* find the same chain in the other map */ 83 | std::pair range = 84 | secondary.equal_range(reverse ? ret->front() : ret->back()); 85 | 86 | WayByTipMap::iterator secondary_iterator; 87 | for (secondary_iterator = range.first; secondary_iterator != range.second; ++secondary_iterator) { 88 | if (secondary_iterator->second == ret) { 89 | break; 90 | } 91 | } 92 | 93 | assert(secondary_iterator != range.second); 94 | 95 | /* delete chain references from both maps */ 96 | secondary.erase(secondary_iterator); 97 | primary.erase(primary_iterator); 98 | 99 | return ret; 100 | } 101 | -------------------------------------------------------------------------------- /libglosm-server/XMLParser.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #include 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | #include 28 | 29 | XMLParser::XMLParser(int flags) : flags_(flags) { 30 | } 31 | 32 | XMLParser::~XMLParser() { 33 | } 34 | 35 | void XMLParser::StartElementWrapper(void* userData, const char* name, const char** atts) { 36 | static_cast(userData)->StartElement(name, atts); 37 | } 38 | 39 | void XMLParser::EndElementWrapper(void* userData, const char* name) { 40 | static_cast(userData)->EndElement(name); 41 | } 42 | 43 | void XMLParser::CharacterDataWrapper(void* userData, const char* data, int len) { 44 | static_cast(userData)->CharacterData(data, len); 45 | } 46 | 47 | void XMLParser::StartElement(const char* /*unused*/, const char** /*unused*/) { 48 | } 49 | 50 | void XMLParser::EndElement(const char* /*unused*/) { 51 | } 52 | 53 | void XMLParser::CharacterData(const char* /*unused*/, int /*unused*/) { 54 | } 55 | 56 | void XMLParser::Load(const char* filename) { 57 | int f = 0; 58 | XML_Parser parser = NULL; 59 | 60 | /* if filename = "-", work with stdin */ 61 | if (strcmp(filename, "-") != 0 && (f = open(filename, O_RDONLY)) == -1) 62 | throw SystemError() << "cannot open input file"; 63 | 64 | /* Create and setup parser */ 65 | if ((parser = XML_ParserCreate(NULL)) == NULL) { 66 | close(f); 67 | throw Exception() << "cannot create XML parser"; 68 | } 69 | 70 | if (flags_ & HANDLE_ELEMENTS) 71 | XML_SetElementHandler(parser, StartElementWrapper, EndElementWrapper); 72 | if (flags_ & HANDLE_CHARDATA) 73 | XML_SetCharacterDataHandler(parser, CharacterDataWrapper); 74 | 75 | XML_SetUserData(parser, this); 76 | 77 | /* Parse file */ 78 | try { 79 | char buf[65536]; 80 | ssize_t len; 81 | do { 82 | if ((len = read(f, buf, sizeof(buf))) < 0) 83 | throw SystemError() << "input read error"; 84 | if (XML_Parse(parser, buf, len, len == 0) == XML_STATUS_ERROR) 85 | throw ParsingException() << XML_ErrorString(XML_GetErrorCode(parser)); 86 | } while (len != 0); 87 | } catch (ParsingException &e) { 88 | ParsingException verbose; 89 | verbose << "input parsing error: " << e.what() << " at line " << XML_GetCurrentLineNumber(parser) << " pos " << XML_GetCurrentColumnNumber(parser); 90 | close(f); 91 | XML_ParserFree(parser); 92 | throw verbose; 93 | } catch (...) { 94 | close(f); 95 | XML_ParserFree(parser); 96 | throw; 97 | } 98 | 99 | XML_ParserFree(parser); 100 | close(f); 101 | } 102 | -------------------------------------------------------------------------------- /libglosm-server/glosm/DummyHeightmap.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef DUMMYHEIGHTMAP_HH 22 | #define DUMMYHEIGHTMAP_HH 23 | 24 | #include 25 | 26 | class DummyHeightmap : public HeightmapDatasource { 27 | protected: 28 | osmint_t height_; 29 | 30 | public: 31 | DummyHeightmap(osmint_t height = 0); 32 | virtual ~DummyHeightmap(); 33 | 34 | virtual void GetHeightmap(const BBoxi& bbox, int extramargin, Heightmap& out) const; 35 | virtual osmint_t GetHeight(const Vector2i& where) const; 36 | }; 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /libglosm-server/glosm/Exception.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef EXCEPTION_HH 22 | #define EXCEPTION_HH 23 | 24 | #include 25 | #include 26 | 27 | namespace Private { 28 | /** 29 | * Safe string buffer to be used in exceptions. 30 | * 31 | * As you may know, std::exception::what() is has no-throw 32 | * exception specifier, thus you should not throw in what(), 33 | * or else the program may terminate during exception 34 | * handling (even if all exceptions are handled properly). 35 | * 36 | * Stock IOstream stringbuf is unsafe from this point of view 37 | * as its str() method does construct string object which may 38 | * throw, so we use our own buffer here. It may be normally 39 | * used with std::ostream, yet it has safe c_str() method which 40 | * never throws. In addition to that, it handles space reserve 41 | * so data may be appended 42 | */ 43 | class SafeStringBuffer : public std::streambuf { 44 | protected: 45 | /** initial size of a buffer */ 46 | static const unsigned int initial_size_ = 64; 47 | 48 | protected: 49 | unsigned int reserve_; 50 | unsigned int allocated_; 51 | unsigned int used_; 52 | char* buffer_; 53 | 54 | protected: 55 | /** 56 | * Ensures that buffer has enough space expanding it if necessary 57 | * 58 | * @param size required buffer size 59 | */ 60 | void EnsureSize(unsigned int size); 61 | 62 | public: 63 | /** 64 | * Constructs empty string buffer 65 | * 66 | * @param reserve specifies length of reserved buffer part 67 | */ 68 | SafeStringBuffer(unsigned int reserve = 0); 69 | 70 | /** 71 | * Constructs buffer and initializes it with giver string 72 | * 73 | * @param message initial value 74 | */ 75 | SafeStringBuffer(const char* message); 76 | 77 | /** 78 | * Constricts copy of other 79 | */ 80 | SafeStringBuffer(const SafeStringBuffer& other); 81 | 82 | /** 83 | * Destructor 84 | */ 85 | ~SafeStringBuffer(); 86 | 87 | /** 88 | * Changes reserve for the buffer expanding it if necessry 89 | * 90 | * @param reserve new reserve value 91 | */ 92 | void SetReserve(unsigned int reserve); 93 | 94 | /** 95 | * Append data to the buffer 96 | * 97 | * This function is used from ostream to append data to the buffer. 98 | * 99 | * @param s pointer to data 100 | * @param n data length 101 | * 102 | * @return number of bytes appended (always n) 103 | */ 104 | std::streamsize xsputn(const char* s, std::streamsize n); 105 | 106 | /** 107 | * Safely append data to the buffer within the reserve 108 | * 109 | * This may be safely used in what() to append additional data 110 | * (such ass strerror) to the end of message. 111 | * 112 | * @param s pointer to data 113 | * @param n data length 114 | * 115 | * @return number of bytes appended 116 | */ 117 | std::streamsize AppendReserve(const char* s, std::streamsize n) throw(); 118 | 119 | /** 120 | * Safely get pointer to buffer contents 121 | * 122 | * @return pointer to null-terminated buffer contents 123 | */ 124 | const char* c_str() throw(); 125 | }; 126 | 127 | /** 128 | * Convenient exception class which supports stream-like appending 129 | * 130 | * Example: 131 | * @code throw Exception("foo") << ", also baz and " << 1 << 2 << 3; @endcode 132 | */ 133 | class ExceptionBase: public std::exception { 134 | protected: 135 | /* being mutable allows appending data to const 136 | * ExceptionBase. See operator<< below for the 137 | * explanation of why const is needed */ 138 | mutable SafeStringBuffer message_; 139 | 140 | public: 141 | /** 142 | * Constructs empty exception 143 | */ 144 | ExceptionBase(); 145 | 146 | /** 147 | * Constructs copy of e 148 | */ 149 | ExceptionBase(const ExceptionBase& e); 150 | 151 | /** 152 | * Destructor 153 | */ 154 | virtual ~ExceptionBase() throw(); 155 | 156 | /** 157 | * Appends data to exception's string buffer 158 | */ 159 | template 160 | void Append(const T& t) const { 161 | std::ostream stream(&message_); 162 | (std::ostream&)stream << t; 163 | } 164 | 165 | /** 166 | * Returns exception buffer contetns as a C string 167 | */ 168 | virtual const char* what() const throw(); 169 | }; 170 | 171 | /** 172 | * Append operator for exception 173 | * 174 | * This just forwards all << calls to Exception which in 175 | * turn forwards them to std::ostream build around 176 | * SafeStringBuffer. 177 | * 178 | * @note This is put under the namespace to not be visible 179 | * from other parts of the program. 180 | * 181 | * @node This is intended to be used on const Exception 182 | * classes because C++ doesn't allow references to non-const 183 | * temporary objects. Pity. However, C++0x brings r-value 184 | * references which can make this look a bit better. 185 | */ 186 | template 187 | static const E& operator<<(const E& e, const T& t) { 188 | e.Append(t); 189 | return e; 190 | } 191 | }; 192 | 193 | /** 194 | * Generic exception 195 | */ 196 | class Exception: public Private::ExceptionBase { 197 | }; 198 | 199 | /** 200 | * System error that automatically appends strerror to the message 201 | */ 202 | class SystemError: public Exception { 203 | protected: 204 | int errno_; 205 | 206 | public: 207 | SystemError(); 208 | SystemError(int errn); 209 | SystemError(const SystemError& e); 210 | virtual ~SystemError() throw(); 211 | virtual const char* what() const throw(); 212 | }; 213 | 214 | #endif 215 | -------------------------------------------------------------------------------- /libglosm-server/glosm/GPXDatasource.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef GPXDATASOURCE_HH 22 | #define GPXDATASOURCE_HH 23 | 24 | #include 25 | #include 26 | 27 | #include 28 | 29 | /** 30 | * Abstract base class for sources of GPX data. 31 | */ 32 | class GPXDatasource { 33 | public: 34 | virtual void GetPoints(std::vector& out, const BBoxi& bbox) const = 0; 35 | }; 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /libglosm-server/glosm/Geometry.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef GEOMETRY_HH 22 | #define GEOMETRY_HH 23 | 24 | #include 25 | #include 26 | 27 | #include 28 | 29 | /** 30 | * 3D geometry in global fixed-point coordinates. 31 | * 32 | * This class represents portable geometry which comes from 33 | * GeometryGenerator and may be stored, serialized/deserialized, 34 | * transferred via the net and in the end is to be converted to 35 | * local floating-point geometry for rendering. 36 | * 37 | * @todo this is pretty non-general approach: triangles and quads 38 | * should likely be merged into a single primitive. Need testing for 39 | * speed impact on quads vs. indexed triangle strips. If strips don't 40 | * bring too buch performance drop, quads should be eliminated. This 41 | * will also open door for further geom optimizations like merging 42 | * triangle groups into strips and fans. 43 | * 44 | * @note just for a note: most of polygon geometry generated for urban 45 | * area currently are quads (~10x more quads than triangles). Changing 46 | * quads to triangle pairs is 12% more geometry generation time, 20% 47 | * less fps and more memory, so for now they're quite useful. 48 | */ 49 | class Geometry { 50 | public: 51 | typedef std::vector VertexVector; 52 | typedef std::vector LengthVector; 53 | 54 | protected: 55 | VertexVector lines_vertices_; 56 | LengthVector lines_lengths_; 57 | 58 | VertexVector convex_vertices_; 59 | LengthVector convex_lengths_; 60 | 61 | public: 62 | Geometry(); 63 | 64 | void AddLine(const Vector3i& a, const Vector3i& b); 65 | void AddTriangle(const Vector3i& a, const Vector3i& b, const Vector3i& c); 66 | void AddQuad(const Vector3i& a, const Vector3i& b, const Vector3i& c, const Vector3i& d); 67 | void AddConvex(const std::vector& v); 68 | void AddLine(const std::vector& v); 69 | 70 | void StartLine(); 71 | void AppendLine(const Vector3i& v); 72 | 73 | void StartConvex(); 74 | void AppendConvex(const Vector3i& v); 75 | 76 | const VertexVector& GetLinesVertices() const; 77 | const LengthVector& GetLinesLengths() const; 78 | 79 | const VertexVector& GetConvexVertices() const; 80 | const LengthVector& GetConvexLengths() const; 81 | 82 | void Append(const Geometry& other); 83 | void AppendCropped(const Geometry& other, const BBoxi& bbox); 84 | 85 | void AddCroppedConvex(const Vector3i* v, unsigned int size, const BBoxi& bbox); 86 | void AddCroppedLine(const Vector3i* v, unsigned int size, const BBoxi& bbox); 87 | 88 | void Serialize() const; 89 | void DeSerialize(); 90 | }; 91 | 92 | #endif 93 | -------------------------------------------------------------------------------- /libglosm-server/glosm/GeometryDatasource.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef GEOMETRYDATASOURCE_HH 22 | #define GEOMETRYDATASOURCE_HH 23 | 24 | #include 25 | #include 26 | 27 | class Geometry; 28 | 29 | /** 30 | * Abstract base class for all Geometry sources including Geometry 31 | * generators. 32 | */ 33 | class GeometryDatasource { 34 | public: 35 | enum Flags { 36 | GROUND = 0x1, 37 | DETAIL = 0x2, 38 | 39 | EVERYTHING = 0x3, 40 | }; 41 | 42 | public: 43 | virtual void GetGeometry(Geometry& geometry, const BBoxi& bbox, int flags = 0) const = 0; 44 | 45 | /** Returns the center of available area */ 46 | virtual Vector2i GetCenter() const { 47 | return Vector2i(0, 0); 48 | } 49 | 50 | /** Returns the bounding box of available area */ 51 | virtual BBoxi GetBBox() const { 52 | return BBoxi::ForEarth(); 53 | } 54 | }; 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /libglosm-server/glosm/GeometryOperations.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef GEOMETRYOPERATIONS_HH 22 | #define GEOMETRYOPERATIONS_HH 23 | 24 | #include 25 | #include 26 | 27 | /** 28 | * Intersect segment with horizontal line 29 | * 30 | * @param one first point of segment 31 | * @param two second point of segment 32 | * @param y y-coordinate for line 33 | * @param out reference to output value 34 | * @return whether there was an intersection 35 | */ 36 | bool IntersectSegmentWithHorizontal(const Vector3i& one, const Vector3i& two, osmint_t y, Vector3i& out); 37 | 38 | /** 39 | * Intersect segment with vertical line 40 | * 41 | * @param one first point of segment 42 | * @param two second point of segment 43 | * @param x x-coordinate of line 44 | * @param out reference to output value 45 | * @return whether there was an intersection 46 | */ 47 | bool IntersectSegmentWithVertical(const Vector3i& one, const Vector3i& two, osmint_t x, Vector3i& out); 48 | 49 | /** 50 | * Intersect segment with one of bounding box sides 51 | * 52 | * @param one first point of segment 53 | * @param two second point of segment 54 | * @param bbox bounding box 55 | * @param side side of bounding box 56 | * @param out reference to output value 57 | * @return whether there was an intersection 58 | */ 59 | bool IntersectSegmentWithBBoxSide(const Vector3i& one, const Vector3i& two, const BBoxi& bbox, BBoxi::Side side, Vector3i& out); 60 | 61 | /** 62 | * Intersect segment with one of bounding box sides (non-inclusive version) 63 | * 64 | * @see IntersectSegmentWithBBoxSide 65 | */ 66 | static inline bool IntersectSegmentWithBBoxSideNI(const Vector3i& one, const Vector3i& two, const BBoxi& bbox, BBoxi::Side side, Vector3i& out) { 67 | return IntersectSegmentWithBBoxSide(one, two, bbox, side, out) && out != one && out != two; 68 | } 69 | 70 | /** 71 | * Intersect segment with bounding box 72 | * 73 | * @param one first point of segment 74 | * @param two second point of segment 75 | * @param bbox bounding box 76 | * @param out reference to output value 77 | * @return whether there was an intersection 78 | */ 79 | BBoxi::Side IntersectSegmentWithBBox(const Vector3i& one, const Vector3i& two, const BBoxi& bbox, Vector3i& out); 80 | 81 | /** 82 | * Intersect segment with bounding box 83 | * 84 | * This function checks intersection with sides of the bounding box 85 | * in the reverse order compared to IntersectSegmentWithBBox, so if 86 | * there are two intersection points, IntersectSegmentWithBBox2 will 87 | * find another one 88 | * 89 | * @param one first point of segment 90 | * @param two second point of segment 91 | * @param bbox bounding box 92 | * @param out reference to output value 93 | * @return whether there was an intersection 94 | */ 95 | BBoxi::Side IntersectSegmentWithBBox2(const Vector3i& one, const Vector3i& two, const BBoxi& bbox, Vector3i& out); 96 | 97 | /** 98 | * Crops segment with bounding box 99 | * 100 | * @param one first point of segment 101 | * @param two second point of segment 102 | * @param bbox bounding box 103 | * @param outone (output) first point of cropped segment 104 | * @param outone (output) second point of cropped segment 105 | * @return whether there was an intersection 106 | */ 107 | bool CropSegmentByBBox(const Vector3i& one, const Vector3i& two, const BBoxi& bbox, Vector3i& outone, Vector3i& outtwo); 108 | 109 | Vector3d ToLocalMetric(const Vector3i& what, const Vector3i& ref); 110 | Vector3i FromLocalMetric(const Vector3d& what, const Vector3i& ref); 111 | 112 | float ApproxDistanceSquare(const BBoxi& bbox, const Vector3i& vec); 113 | 114 | #endif 115 | -------------------------------------------------------------------------------- /libglosm-server/glosm/Guard.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef GUARD_HH 22 | #define GUARD_HH 23 | 24 | #include 25 | 26 | /** 27 | * Simple guard class 28 | */ 29 | class Guard { 30 | public: 31 | Guard(pthread_mutex_t& mutex); 32 | ~Guard(); 33 | 34 | protected: 35 | pthread_mutex_t& mutex_; 36 | }; 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /libglosm-server/glosm/HeightmapDatasource.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef HEIGHTMAPDATASOURCE_HH 22 | #define HEIGHTMAPDATASOURCE_HH 23 | 24 | #include 25 | #include 26 | 27 | #include 28 | 29 | /** 30 | * Abstract base class for sources of heightmap data. 31 | */ 32 | class HeightmapDatasource { 33 | public: 34 | struct Heightmap { 35 | typedef std::vector HeightmapPoints; 36 | 37 | HeightmapPoints points; 38 | int width; 39 | int height; 40 | 41 | BBoxi bbox; 42 | }; 43 | 44 | public: 45 | virtual ~HeightmapDatasource() {} 46 | 47 | virtual void GetHeightmap(const BBoxi& bbox, int extramargin, Heightmap& out) const = 0; 48 | virtual osmint_t GetHeight(const Vector2i& where) const = 0; 49 | }; 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /libglosm-server/glosm/Misc.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef MISC_HH 22 | #define MISC_HH 23 | 24 | /* collection of reinvented bicycles; it's better than using weighty 25 | * boost while it's small enough */ 26 | 27 | #define COMPILE_TIME_ASSERT(V) \ 28 | { typedef int __libglosm_compile_time_assert_fail[1 - 2*!(V)]; } 29 | 30 | static inline bool IsBigEndian() { 31 | static const union { 32 | unsigned char bytes[4]; 33 | uint32_t value; 34 | } endianess_checker = { 35 | { 0, 1, 2, 3 } 36 | }; 37 | 38 | return endianess_checker.value == 0x00010203UL; 39 | } 40 | 41 | static inline bool IsLittleEndian() { 42 | static const union { 43 | unsigned char bytes[4]; 44 | uint32_t value; 45 | } endianess_checker = { 46 | { 0, 1, 2, 3 } 47 | }; 48 | 49 | return endianess_checker.value == 0x03020100UL; 50 | } 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /libglosm-server/glosm/NonCopyable.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef NONCOPYABLE_HH 22 | #define NONCOPYABLE_HH 23 | 24 | /** 25 | * A convenient way to disable copying of objects of specific classes 26 | */ 27 | class NonCopyable { 28 | protected: 29 | NonCopyable() { 30 | } 31 | 32 | private: 33 | NonCopyable(const NonCopyable&); 34 | NonCopyable& operator =(const NonCopyable&); 35 | }; 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /libglosm-server/glosm/OsmDatasource.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef OSMDATASOURCE_HH 22 | #define OSMDATASOURCE_HH 23 | 24 | #include 25 | #include 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | /** 32 | * Abstract base class for sources of OpenStreetMap data. 33 | * 34 | * Provides a way for other components to request map data in the 35 | * form of nodes, ways and relations. 36 | */ 37 | class OsmDatasource { 38 | public: 39 | typedef std::map TagsMap; 40 | 41 | public: 42 | struct Node { 43 | Vector2i Pos; 44 | 45 | Node() {} 46 | Node(int x, int y) : Pos(x, y) {} 47 | }; 48 | 49 | struct Way { 50 | typedef std::vector NodesList; 51 | 52 | NodesList Nodes; 53 | 54 | TagsMap Tags; 55 | bool Closed; 56 | bool Clockwise; 57 | 58 | BBoxi BBox; 59 | 60 | Way() : Closed(false), Clockwise(false), BBox(BBoxi::Empty()) { 61 | } 62 | }; 63 | 64 | struct Relation { 65 | struct Member { 66 | enum Type_t { 67 | WAY, 68 | NODE, 69 | RELATION, 70 | 71 | UNKNOWN, 72 | } Type; 73 | 74 | osmid_t Ref; 75 | std::string Role; 76 | 77 | Member(Type_t type, osmid_t ref, const char* role): Type(type), Ref(ref), Role(role) {} 78 | }; 79 | 80 | typedef std::vector MemberList; 81 | 82 | MemberList Members; 83 | TagsMap Tags; 84 | }; 85 | 86 | public: 87 | /** Returns node by its id */ 88 | virtual const Node& GetNode(osmid_t id) const = 0; 89 | 90 | /** Returns way by its id */ 91 | virtual const Way& GetWay(osmid_t id) const = 0; 92 | 93 | /** Returns relation by its id */ 94 | virtual const Relation& GetRelation(osmid_t id) const = 0; 95 | 96 | /* multiple - object accessors subject to change */ 97 | virtual void GetWays(std::vector& out, const BBoxi& bbox) const = 0; 98 | 99 | /** Returns center of available area */ 100 | virtual Vector2i GetCenter() const { 101 | return Vector2i(0, 0); 102 | } 103 | 104 | /** Returns bounding box of available area */ 105 | virtual BBoxi GetBBox() const { 106 | return BBoxi::ForEarth(); 107 | } 108 | }; 109 | 110 | #endif 111 | -------------------------------------------------------------------------------- /libglosm-server/glosm/ParsingHelpers.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef PARSINGHELPERS_HH 22 | #define PARSINGHELPERS_HH 23 | 24 | #include 25 | #include 26 | 27 | #include 28 | 29 | #ifdef TRUSTED_XML 30 | /* this is a shortcut for string comparison used where we know 31 | * that the string e.g. bay only be 'way', 'node', or 'relation' 32 | * in which case we can only check first letter - this lends 33 | * ~10% performance gain */ 34 | template 35 | inline bool StrEq(const char* one, const char* two) { 36 | return strncmp(one, two, I) == 0; 37 | } 38 | 39 | template<> 40 | inline bool StrEq<-1>(const char* one, const char* two) { 41 | return strcmp(one, two) == 0; 42 | } 43 | 44 | template<> 45 | inline bool StrEq<0>(const char* one, const char* two) { 46 | return true; 47 | } 48 | 49 | template<> 50 | inline bool StrEq<1>(const char* one, const char* two) { 51 | return one[0] == two[0]; 52 | } 53 | 54 | template<> 55 | inline bool StrEq<2>(const char* one, const char* two) { 56 | return one[0] == two[0] && one[0] != '\0' && one[1] == two[1]; 57 | } 58 | #else 59 | template 60 | inline bool StrEq(const char* one, const char* two) { 61 | return strcmp(one, two) == 0; 62 | } 63 | #endif 64 | 65 | /** 66 | * Parses decimal floating point number into fixed-point value 67 | */ 68 | template 69 | static int ParseInt(const char* str) { 70 | int value = 0; 71 | int fracdig = 0; 72 | int haddot = 0; 73 | bool neg = false; 74 | const char* cur = str; 75 | 76 | if (*cur == '-') { 77 | neg = true; 78 | cur++; 79 | } 80 | 81 | for (; *cur != '\0'; ++cur) { 82 | if (*cur >= '0' && *cur <= '9') { 83 | value = value * 10 + *cur - '0'; 84 | if (haddot && ++fracdig == I) 85 | break; 86 | } else if (*cur == '.') { 87 | haddot++; 88 | } else { 89 | throw ParsingException() << "bad coordinate format (unexpected symbol)"; 90 | } 91 | } 92 | 93 | if (haddot > 1) 94 | throw ParsingException() << "bad coordinate format (multiple dots)"; 95 | 96 | for (; fracdig < I; ++fracdig) 97 | value *= 10; 98 | 99 | return neg ? -value : value; 100 | } 101 | 102 | /** 103 | * Parses langitude/latitude into fixed-point number 104 | */ 105 | int ParseCoord(const char* str); 106 | 107 | /** 108 | * Parses elevation into fixed-point number 109 | */ 110 | int ParseEle(const char* str); 111 | 112 | /** 113 | * Parses attributes of tag 114 | * 115 | * This tag is used in both OSM and GPX files to denote bounding 116 | * box of a dataset 117 | * 118 | * @param atts tag attrubutes from XML parser 119 | * @return resulting bbox 120 | */ 121 | BBoxi ParseBounds(const char** atts); 122 | 123 | /** 124 | * Parses attributes of tag 125 | * 126 | * This tag is encountered in OSM files produced by osmosis. 127 | * 128 | * @param atts tag attrubutes from XML parser 129 | * @return resulting bbox 130 | */ 131 | BBoxi ParseBound(const char** atts); 132 | 133 | #endif 134 | -------------------------------------------------------------------------------- /libglosm-server/glosm/PreloadedGPXDatasource.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef PRELOADEDGPXDATASOURCE_HH 22 | #define PRELOADEDGPXDATASOURCE_HH 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | #include 29 | 30 | /** 31 | * Source of GPX data which preloads tracks into memory. 32 | * 33 | * @todo this needs quadtree or some other clever interface 34 | * to scale for large amounts of data. 35 | */ 36 | class PreloadedGPXDatasource : public XMLParser, public GPXDatasource, private NonCopyable { 37 | protected: 38 | typedef std::vector PointsVector; 39 | 40 | protected: 41 | PointsVector points_; 42 | 43 | /* parser state */ 44 | enum { 45 | NONE, 46 | GPX, 47 | TRK, 48 | TRKSEG, 49 | TRKPT, 50 | ELE, 51 | } current_tag_; 52 | 53 | int tag_level_; 54 | 55 | protected: 56 | /** 57 | * Processes start XML element 58 | */ 59 | virtual void StartElement(const char* name, const char** atts); 60 | 61 | /** 62 | * Processes end XML element 63 | */ 64 | virtual void EndElement(const char* name); 65 | 66 | /** 67 | * Processes end XML element 68 | */ 69 | virtual void CharacterData(const char* data, int len); 70 | 71 | public: 72 | /** 73 | * Constructs empty datasource 74 | */ 75 | PreloadedGPXDatasource(); 76 | 77 | /** 78 | * Destructor 79 | */ 80 | virtual ~PreloadedGPXDatasource(); 81 | 82 | /** 83 | * Parses OSM dump file and loads map data into memory 84 | * 85 | * @param filename path to dump file 86 | */ 87 | virtual void Load(const char* filename); 88 | 89 | virtual void GetPoints(std::vector& out, const BBoxi& bbox) const; 90 | }; 91 | 92 | #endif 93 | -------------------------------------------------------------------------------- /libglosm-server/glosm/PreloadedXmlDatasource.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef PRELOADEDXMLDATASOURCE_HH 22 | #define PRELOADEDXMLDATASOURCE_HH 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | /** 31 | * Excepion that denotes inconsistent OSM data 32 | */ 33 | class DataException : public Exception { 34 | }; 35 | 36 | /** 37 | * Source of OpenStreetMap data which preloads .osm dump into memory. 38 | * 39 | * This is a simple OsmDatasource that first parses OSM XML dump 40 | * with XML parser and stores node/way/relation information in 41 | * memory. 42 | */ 43 | class PreloadedXmlDatasource : public XMLParser, public OsmDatasource, private NonCopyable { 44 | protected: 45 | enum CurrentTag { 46 | NONE, 47 | OSM, /* root */ 48 | NODE, 49 | WAY, 50 | RELATION, 51 | }; 52 | 53 | protected: 54 | typedef id_map NodesMap; 55 | //typedef id_map NodeTagsMap; 56 | typedef id_map WaysMap; 57 | typedef id_map RelationsMap; 58 | 59 | protected: 60 | /* data */ 61 | NodesMap nodes_; 62 | //NodeTagsMap node_tags_; 63 | WaysMap ways_; 64 | RelationsMap relations_; 65 | 66 | /* parser state */ 67 | CurrentTag current_tag_; 68 | int tag_level_; 69 | 70 | NodesMap::iterator last_node_; 71 | //NodeTagsMap::iterator last_node_tags_; 72 | WaysMap::iterator last_way_; 73 | RelationsMap::iterator last_relation_; 74 | 75 | BBoxi bbox_; 76 | 77 | /* id counter for syntheric objects; goes down from max possible ID */ 78 | static osmid_t next_synthetic_id_; 79 | 80 | protected: 81 | /** 82 | * Processes start XML element 83 | */ 84 | virtual void StartElement(const char* name, const char** atts); 85 | 86 | /** 87 | * Processes end XML element 88 | */ 89 | virtual void EndElement(const char* name); 90 | 91 | protected: 92 | /** 93 | * Extra processing for ways 94 | */ 95 | void FinalizeWay(); 96 | 97 | /** 98 | * Extra processing for relations 99 | */ 100 | void FinalizeRelation(); 101 | 102 | public: 103 | /** 104 | * Constructs empty datasource 105 | */ 106 | PreloadedXmlDatasource(); 107 | 108 | /** 109 | * Destructor 110 | */ 111 | virtual ~PreloadedXmlDatasource(); 112 | 113 | /** 114 | * Parses OSM dump file and loads map data into memory 115 | * 116 | * @param filename path to dump file 117 | */ 118 | virtual void Load(const char* filename); 119 | 120 | /** 121 | * Drops all loaded data 122 | * 123 | * This is used to free memory used by loaded data when 124 | * it's no longer needed. Datasource may be further reused. 125 | */ 126 | void Clear(); 127 | 128 | /** 129 | * Returns center of loaded dump 130 | * 131 | * Center is calculated from bounding box 132 | * 133 | * @see GetBBox() 134 | */ 135 | virtual Vector2i GetCenter() const; 136 | 137 | /** 138 | * Returns bounding box of loaded dump 139 | * 140 | * BBox is taken from either or tag, or, 141 | * in absence of these, is automatically calculated to covert 142 | * all available nodes 143 | */ 144 | virtual BBoxi GetBBox() const; 145 | 146 | public: 147 | virtual const Node& GetNode(osmid_t id) const; 148 | virtual const Way& GetWay(osmid_t id) const; 149 | virtual const Relation& GetRelation(osmid_t id) const; 150 | 151 | virtual void GetWays(std::vector& out, const BBoxi& bbox) const; 152 | }; 153 | 154 | #endif 155 | -------------------------------------------------------------------------------- /libglosm-server/glosm/SRTMDatasource.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef SRTMDATASOURCE_HH 22 | #define SRTMDATASOURCE_HH 23 | 24 | #include 25 | 26 | #include 27 | #include 28 | 29 | #include 30 | #include 31 | 32 | class SRTMDatasource : public HeightmapDatasource { 33 | protected: 34 | struct ChunkId { 35 | short lon; 36 | short lat; 37 | 38 | ChunkId(short lo, short la): lon(lo), lat(la) { 39 | } 40 | 41 | bool operator< (const ChunkId& other) const { 42 | return lon < other.lon || (lon == other.lon && lat < other.lat); 43 | } 44 | }; 45 | 46 | struct Chunk { 47 | int generation; 48 | std::vector data; 49 | }; 50 | 51 | protected: 52 | typedef std::map ChunksMap; 53 | 54 | protected: 55 | const char* storage_path_; 56 | mutable int generation_; 57 | 58 | mutable pthread_mutex_t mutex_; 59 | 60 | mutable ChunksMap chunks_; 61 | 62 | protected: 63 | Chunk& RequireChunk(int lon, int lat) const; 64 | osmint_t GetPointHeight(int x, int y) const; 65 | 66 | public: 67 | SRTMDatasource(const char* storage_path); 68 | virtual ~SRTMDatasource(); 69 | 70 | virtual void GetHeightmap(const BBoxi& bbox, int extramargin, Heightmap& out) const; 71 | virtual osmint_t GetHeight(const Vector2i& where) const; 72 | }; 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /libglosm-server/glosm/Timer.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef TIMER_HH 22 | #define TIMER_HH 23 | 24 | #include 25 | 26 | /** 27 | * Simple timer class 28 | */ 29 | class Timer { 30 | public: 31 | /** 32 | * Constructs timer and initializes it with current time 33 | */ 34 | Timer(); 35 | 36 | /** 37 | * Updates timer and returns time passed 38 | * 39 | * @return seconds passed since construction or last Count() 40 | * call 41 | */ 42 | float Count(); 43 | 44 | protected: 45 | struct timeval last_; 46 | }; 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /libglosm-server/glosm/WayMerger.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef WAYMERGER_HH 22 | #define WAYMERGER_HH 23 | 24 | #include 25 | #include 26 | 27 | #include 28 | 29 | /** 30 | * Class that merges complete ways from parts 31 | * 32 | * @note this currently only works with closed ways, but a 33 | * flag(s) may be added to support e.g routes 34 | */ 35 | class WayMerger { 36 | protected: 37 | typedef std::multimap WayByTipMap; 38 | 39 | typedef std::vector NodeChain; 40 | typedef std::vector NodeChainVector; 41 | 42 | protected: 43 | WayByTipMap way_by_node; 44 | WayByTipMap way_by_node_rev; 45 | 46 | public: 47 | WayMerger(); 48 | 49 | void AddWay(const OsmDatasource::Way::NodesList& nodes); 50 | 51 | bool GetNextWay(OsmDatasource::Way::NodesList& outnodes); 52 | 53 | protected: 54 | const OsmDatasource::Way::NodesList* PickChain(osmid_t id = 0, bool reverse = false); 55 | }; 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /libglosm-server/glosm/XMLParser.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef XMLPARSER_HH 22 | #define XMLPARSER_HH 23 | 24 | #include 25 | 26 | /** 27 | * Excepion that denotes XML parsing error 28 | */ 29 | class ParsingException : public Exception { 30 | }; 31 | 32 | /** 33 | * Base class for all XML parsers 34 | */ 35 | class XMLParser { 36 | protected: 37 | enum Flags { 38 | HANDLE_ELEMENTS = 0x01, 39 | HANDLE_CHARDATA = 0x02, 40 | 41 | HANDLE_ALL = 0xFF, 42 | }; 43 | 44 | int flags_; 45 | 46 | protected: 47 | /** 48 | * Static wrapper for StartElement 49 | */ 50 | static void StartElementWrapper(void* userData, const char* name, const char** atts); 51 | 52 | /** 53 | * Static wrapper for EndElement 54 | */ 55 | static void EndElementWrapper(void* userData, const char* name); 56 | 57 | /** 58 | * Static wrapper for CharacterData 59 | */ 60 | static void CharacterDataWrapper(void* userData, const char* data, int len); 61 | 62 | /** 63 | * Processes start of XML element 64 | */ 65 | virtual void StartElement(const char* name, const char** atts); 66 | 67 | /** 68 | * Processes end of XML element 69 | */ 70 | virtual void EndElement(const char* name); 71 | 72 | /** 73 | * Processes character data 74 | */ 75 | virtual void CharacterData(const char* data, int len); 76 | 77 | protected: 78 | /** 79 | * Constructs empty datasource 80 | */ 81 | XMLParser(int flags = HANDLE_ALL); 82 | 83 | /** 84 | * Destructor 85 | */ 86 | virtual ~XMLParser(); 87 | 88 | public: 89 | /** 90 | * Parses OSM dump file and loads map data into memory 91 | * 92 | * @param filename path to dump file 93 | */ 94 | virtual void Load(const char* filename); 95 | }; 96 | 97 | #endif 98 | -------------------------------------------------------------------------------- /libglosm-server/glosm/geomath.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef GEOMATH_H 22 | #define GEOMATH_H 23 | 24 | #include 25 | 26 | /* Earth equatorial radius in WGS84 */ 27 | #define WGS84_EARTH_EQ_RADIUS 6378137.0 28 | 29 | /* Earth equatorial length */ 30 | #define WGS84_EARTH_EQ_LENGTH (M_PI * 2.0 * WGS84_EARTH_EQ_RADIUS) 31 | 32 | /* Earth polar radius in WGS84 */ 33 | #define WGS84_EARTH_POLE_RADIUS 6356752.314245 34 | 35 | /* Zero meridian offset (rel to Greenwich) in WGS84 */ 36 | #define WGS84_LONGITUDE_OFFSET 5.31/3600.0 37 | 38 | /* Portable geometry constants */ 39 | #define GEOM_UNITSINDEGREE 10000000 40 | #define GEOM_UNITSINMETER 100 41 | 42 | #define GEOM_MINLON (-180 * GEOM_UNITSINDEGREE) 43 | #define GEOM_MAXLON ( 180 * GEOM_UNITSINDEGREE) 44 | #define GEOM_MINLAT (-90 * GEOM_UNITSINDEGREE) 45 | #define GEOM_MAXLAT ( 90 * GEOM_UNITSINDEGREE) 46 | 47 | #define GEOM_LONSPAN (360U * GEOM_UNITSINDEGREE) 48 | #define GEOM_LATSPAN (180 * GEOM_UNITSINDEGREE) 49 | 50 | #define GEOM_MERCATOR_MINLAT (-85 * GEOM_UNITSINDEGREE) 51 | #define GEOM_MERCATOR_MAXLAT ( 85 * GEOM_UNITSINDEGREE) 52 | 53 | #define GEOM_DEG_TO_RAD (M_PI / (double)(180 * GEOM_UNITSINDEGREE)) 54 | #define GEOM_RAD_TO_DEG ((double)(180 * GEOM_UNITSINDEGREE) / M_PI) 55 | 56 | /* 57 | * These were chosen out of 7 mercator and 4 unmercator 58 | * functions as fastest on common i386/amd64 hardware; these also 59 | * happen to give moderate precision. For specific purpose (e.g. 60 | * maximal performance on specific hardware or top precision, other 61 | * functions may be chosen. 62 | */ 63 | 64 | /** Mercator projection */ 65 | inline double mercator(double x) { 66 | return 0.5*log((1.0+sin(x))/(1.0-sin(x))); 67 | } 68 | 69 | /** Mercator un-projection */ 70 | inline double unmercator(double x) { 71 | return 2.0*atan(tanh(0.5*x)); 72 | } 73 | 74 | #endif // GEOMATH_H 75 | -------------------------------------------------------------------------------- /libglosm-server/glosm/osmtypes.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef OSMTYPES_H 22 | #define OSMTYPES_H 23 | 24 | #include 25 | 26 | /* Should be enough to hold max id for any osm object */ 27 | typedef int64_t osmid_t; 28 | 29 | /* Holds fixed point coordinate in range [-1'800'000'000; 1'800'000'000] 30 | * 32 bit is not enough to hold difference between coordinates so 31 | * convertsion to osmcoord_long_t is required in that case; 32 | * This is also used for storing height with 1mm resolution */ 33 | typedef signed int osmint_t; 34 | 35 | /* Useful for results of operations on osmcoord_t which won't stick 36 | * into osmcoord_t, such as difference between points and vector 37 | * operations involving multiplication */ 38 | typedef signed long long osmlong_t; 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | INCLUDE_DIRECTORIES(../libglosm-client ../libglosm-server) 2 | 3 | # Targets 4 | ADD_EXECUTABLE(ProjectionTest ProjectionTest.cc) 5 | TARGET_LINK_LIBRARIES(ProjectionTest glosm-server glosm-client) 6 | 7 | ADD_EXECUTABLE(ProjectionBench ProjectionBench.cc) 8 | TARGET_LINK_LIBRARIES(ProjectionBench glosm-server glosm-client) 9 | 10 | ADD_EXECUTABLE(TypeTest TypeTest.cc) 11 | TARGET_LINK_LIBRARIES(TypeTest glosm-server) 12 | 13 | ADD_EXECUTABLE(ExceptionTest ExceptionTest.cc) 14 | TARGET_LINK_LIBRARIES(ExceptionTest glosm-server) 15 | 16 | ADD_EXECUTABLE(IdMapTest IdMapTest.cc) 17 | 18 | # Tests 19 | ADD_TEST(ProjectionTest ProjectionTest) 20 | ADD_TEST(TypeTest TypeTest) 21 | ADD_TEST(ExceptionTest ExceptionTest) 22 | ADD_TEST(IdMapTest IdMapTest) 23 | -------------------------------------------------------------------------------- /tests/ExceptionTest.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | /* 22 | * This test checks exception functionality. 23 | */ 24 | 25 | #include 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | class MyException : public Exception { 32 | }; 33 | 34 | int main() { 35 | int ret = 0; 36 | 37 | /* Test generic stream-like behaviour */ 38 | try { 39 | throw Exception() << "foo " << 123 << " bar"; 40 | } catch (Exception& e) { 41 | std::cerr << "Caught right exception: " << e.what() << std::endl; 42 | if (strcmp(e.what(), "foo 123 bar") != 0) { 43 | std::cerr << "But the message is wrong" << std::endl; 44 | ret = 1; 45 | } 46 | } catch (...) { 47 | std::cerr << "Caught wrong exception" << std::endl; 48 | ret = 1; 49 | } 50 | 51 | /* Test proper inheritance: return type of operator<< 52 | * should be the save as original exception */ 53 | try { 54 | throw MyException(); 55 | } catch (MyException& e) { 56 | std::cerr << "Caught right exception: " << std::endl; 57 | } catch (...) { 58 | std::cerr << "Caught wrong exception" << std::endl; 59 | ret = 1; 60 | } 61 | 62 | /* Test system errors. Message should be appended */ 63 | try { 64 | errno = EINVAL; 65 | throw SystemError() << "TEST" << 123; 66 | } catch (std::exception& e) { 67 | std::cerr << "Caught right exception: " << e.what() << std::endl; 68 | if (strstr(e.what(), "TEST123") == NULL) { 69 | std::cerr << "But user message was lost" << std::endl; 70 | ret = 1; 71 | } 72 | if (strstr(e.what(), strerror(errno)) == NULL) { 73 | std::cerr << "But system message was lost" << std::endl; 74 | ret = 1; 75 | } 76 | } catch (...) { 77 | std::cerr << "Caught wrong exception" << std::endl; 78 | ret = 1; 79 | } 80 | 81 | /* Test that special exception (system error) may be cast to 82 | * generic one without loosing any data */ 83 | try { 84 | errno = EINVAL; 85 | throw Exception(SystemError() << "TEST" << 123); 86 | } catch (std::exception& e) { 87 | std::cerr << "Caught right exception: " << e.what() << std::endl; 88 | if (strstr(e.what(), "TEST123") == NULL) { 89 | std::cerr << "But user message was lost" << std::endl; 90 | ret = 1; 91 | } 92 | if (strstr(e.what(), strerror(errno)) == NULL) { 93 | std::cerr << "But system message was lost" << std::endl; 94 | ret = 1; 95 | } 96 | } catch (...) { 97 | std::cerr << "Caught wrong exception" << std::endl; 98 | ret = 1; 99 | } 100 | 101 | return ret; 102 | } 103 | -------------------------------------------------------------------------------- /tests/IdMapTest.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | /* 22 | * This test checks whether types of enough length are used for 23 | * vector operations. 24 | * 25 | * Thus, when coordinates are stored as 32-bit values, 64 bits 26 | * should be used in calculations, as 32 bits are not enough to 27 | * hold the result of addition/substraction and especially 28 | * multiplication of two 32 bit values. 29 | */ 30 | 31 | #include 32 | 33 | #include "testing.h" 34 | 35 | typedef id_map TestMap; 36 | 37 | BEGIN_TEST() 38 | // create 39 | TestMap map(8); 40 | 41 | EXPECT_INT(map.size(), 0); 42 | EXPECT_TRUE(map.empty()); 43 | EXPECT_TRUE(map.begin() == map.end()); 44 | 45 | // fill 46 | int ksum = 0, vsum = 0; 47 | { 48 | for (unsigned int i = 0; i < 8; ++i) { 49 | std::pair res = map.insert(std::make_pair(i, 1024 - i)); 50 | EXPECT_TRUE(res.second); 51 | EXPECT_TRUE(res.first->first == i && res.first->second == 1024 - i); 52 | ksum += i; 53 | vsum += 1024 - i; 54 | } 55 | 56 | EXPECT_INT(map.size(), 8); 57 | EXPECT_TRUE(!map.empty()); 58 | } 59 | 60 | // read 61 | { 62 | int testksum = 0, testvsum = 0, iterations = 0; 63 | for (TestMap::const_iterator i = map.begin(); i != map.end(); ++i, ++iterations) { 64 | testksum += i->first; 65 | testvsum += i->second; 66 | } 67 | 68 | EXPECT_INT(iterations, 8); 69 | EXPECT_INT(testvsum, vsum); 70 | EXPECT_INT(testksum, ksum); 71 | } 72 | 73 | // check miss 74 | EXPECT_TRUE(map.find(8) == map.end()); 75 | 76 | // find 77 | { 78 | int testksum = 0, testvsum = 0; 79 | for (unsigned int i = 0; i < 8; ++i) { 80 | TestMap::iterator el = map.find(i); 81 | EXPECT_TRUE(el != map.end()); 82 | assert(el != map.end()); 83 | testksum += el->first; 84 | testvsum += el->second; 85 | } 86 | 87 | EXPECT_INT(testvsum, vsum); 88 | EXPECT_INT(testksum, ksum); 89 | } 90 | 91 | // fill extra, will trigger rehashes and page allocs 92 | { 93 | for (unsigned int i = 8; i < 32; ++i) { 94 | std::pair res = map.insert(std::make_pair(i, 1024 - i)); 95 | EXPECT_TRUE(res.second); 96 | EXPECT_TRUE(res.first->first == i && res.first->second == 1024 - i); 97 | ksum += i; 98 | vsum += 1024 - i; 99 | } 100 | 101 | EXPECT_INT(map.size(), 32); 102 | EXPECT_TRUE(!map.empty()); 103 | } 104 | 105 | // read 106 | { 107 | int testksum = 0, testvsum = 0, iterations = 0; 108 | for (TestMap::const_iterator i = map.begin(); i != map.end(); ++i, ++iterations) { 109 | testksum += i->first; 110 | testvsum += i->second; 111 | } 112 | 113 | EXPECT_INT(iterations, 32); 114 | EXPECT_INT(testvsum, vsum); 115 | EXPECT_INT(testksum, ksum); 116 | } 117 | 118 | // find 119 | { 120 | int testksum = 0, testvsum = 0; 121 | for (unsigned int i = 0; i < 32; ++i) { 122 | TestMap::iterator el = map.find(i); 123 | EXPECT_TRUE(el != map.end()); 124 | assert(el != map.end()); 125 | testksum += el->first; 126 | testvsum += el->second; 127 | } 128 | 129 | EXPECT_INT(testvsum, vsum); 130 | EXPECT_INT(testksum, ksum); 131 | } 132 | 133 | // pop 134 | for (unsigned int i = 31; i >= 4; --i) { 135 | map.erase_last(); 136 | ksum -= i; 137 | vsum -= 1024 - i; 138 | } 139 | 140 | // read 141 | { 142 | int testksum = 0, testvsum = 0, iterations = 0; 143 | for (TestMap::const_iterator i = map.begin(); i != map.end(); ++i, ++iterations) { 144 | testksum += i->first; 145 | testvsum += i->second; 146 | } 147 | 148 | EXPECT_INT(iterations, 4); 149 | EXPECT_INT(testvsum, vsum); 150 | EXPECT_INT(testksum, ksum); 151 | } 152 | 153 | // find 154 | { 155 | int testksum = 0, testvsum = 0; 156 | for (unsigned int i = 0; i < 4; ++i) { 157 | TestMap::iterator el = map.find(i); 158 | EXPECT_TRUE(el != map.end()); 159 | assert(el != map.end()); 160 | testksum += el->first; 161 | testvsum += el->second; 162 | } 163 | 164 | EXPECT_INT(testvsum, vsum); 165 | EXPECT_INT(testksum, ksum); 166 | 167 | for (unsigned int i = 4; i < 33; ++i) { 168 | TestMap::iterator el = map.find(i); 169 | EXPECT_TRUE(el == map.end()); 170 | } 171 | } 172 | 173 | END_TEST() 174 | -------------------------------------------------------------------------------- /tests/ProjectionBench.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | /* 22 | * This is a microbenchmark for projections. 23 | */ 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | #include 30 | 31 | #include 32 | #include 33 | 34 | void ProjBench(Projection projection) { 35 | int count = 0; 36 | 37 | struct timeval start, end; 38 | gettimeofday(&start, NULL); 39 | for (int lon = GEOM_MINLON; lon <= GEOM_MAXLON; lon += GEOM_UNITSINDEGREE/4) { 40 | for (int lat = GEOM_MINLAT; lat <= GEOM_MAXLAT; lat += GEOM_UNITSINDEGREE/4) { 41 | projection.Project(Vector3i(lon, lat, 10), Vector3i(GEOM_UNITSINDEGREE * 45, GEOM_UNITSINDEGREE * 60, 10)); 42 | projection.Project(Vector3i(lon, lat, 10), Vector3i(GEOM_UNITSINDEGREE * -45, GEOM_UNITSINDEGREE * -60, 10)); 43 | projection.Project(Vector3i(lon, lat, 10), Vector3i(GEOM_UNITSINDEGREE * 45, GEOM_UNITSINDEGREE * -60, 10)); 44 | projection.Project(Vector3i(lon, lat, 10), Vector3i(GEOM_UNITSINDEGREE * -45, GEOM_UNITSINDEGREE * 60, 10)); 45 | count += 4; 46 | } 47 | } 48 | gettimeofday(&end, NULL); 49 | 50 | float sec = (float)(end.tv_sec - start.tv_sec) + (float)(end.tv_usec - start.tv_usec)/1000000.0f; 51 | 52 | fprintf(stderr, " %d points, %f seconds, %f points per second\n", count, sec, (float)count/sec); 53 | } 54 | 55 | int main() { 56 | fprintf(stderr, "MercatorProjection:\n"); 57 | ProjBench(MercatorProjection()); 58 | 59 | fprintf(stderr, "SphericalProjection:\n"); 60 | ProjBench(SphericalProjection()); 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /tests/ProjectionTest.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | /* 22 | * This test projects a point using Mercator projection, then 23 | * unprojects it. Result of project/unproject should match the 24 | * original point if the point is not far from origin. 25 | */ 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | #include 32 | #include 33 | 34 | int ProjTest(Projection projection) { 35 | osmint_t xref[] = { -1350000000, -450000000, 0, 450000000, 1350000000 }; 36 | osmint_t yref[] = { -700000000, -450000000, 0, 450000000, 700000000 }; 37 | 38 | Vector3i deltas[] = { 39 | Vector3i(0, 0, 0), 40 | 41 | Vector3i(1, 1, 10000), 42 | Vector3i(1, -1, 100000), 43 | Vector3i(-1, -1, 100000), 44 | Vector3i(-1, 1, 100000), 45 | 46 | Vector3i(1000, 1000, 10000), 47 | Vector3i(1000, -1000, 100000), 48 | Vector3i(-1000, -1000, 100000), 49 | Vector3i(-1000, 1000, 100000), 50 | 51 | /* +/- 100km on equator; if there's no error within this range, we can safely do Z8 geom tiles */ 52 | Vector3i(9000000, 9000000, 10000), 53 | Vector3i(9000000, -9000000, 10000), 54 | Vector3i(-9000000, -9000000, 10000), 55 | Vector3i(-9000000, 9000000, 10000), 56 | }; 57 | 58 | bool result = 0; 59 | 60 | for (unsigned int ixref = 0; ixref < sizeof(xref)/sizeof(xref[0]); ++ixref) { 61 | for (unsigned int iyref = 0; iyref < sizeof(yref)/sizeof(yref[0]); ++iyref) { 62 | for (unsigned int idelta = 0; idelta < sizeof(deltas)/sizeof(deltas[0]); ++idelta) { 63 | Vector3i ref = Vector3i(xref[ixref], yref[iyref], 0); 64 | Vector3i orig = ref + deltas[idelta]; 65 | 66 | Vector3f projected = projection.Project(orig, ref); 67 | Vector3i unprojected = projection.UnProject(projected, ref); 68 | 69 | Vector3i error = unprojected - orig; 70 | error.x = abs(error.x); 71 | error.y = abs(error.y); 72 | error.z = abs(error.z); 73 | 74 | int maxerror = std::max(error.x, std::max(error.y, error.z)); 75 | 76 | printf("[%d, %d, %d]\n by [%d, %d, %d]\n -> [%.10f, %.10f, %.10f]\n -> [%d, %d, %d]\n error=%d\n", 77 | orig.x, orig.y, orig.z, 78 | ref.x, ref.y, ref.z, 79 | projected.x, projected.y, projected.z, 80 | unprojected.x, unprojected.y, unprojected.z, 81 | maxerror 82 | ); 83 | 84 | if (maxerror > 0) 85 | result = 1; 86 | } 87 | } 88 | } 89 | 90 | return result; 91 | } 92 | 93 | int main() { 94 | int result = 0; 95 | 96 | result |= ProjTest(MercatorProjection()); 97 | result |= ProjTest(SphericalProjection()); 98 | 99 | return result; 100 | } 101 | -------------------------------------------------------------------------------- /tests/TypeTest.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | /* 22 | * This test checks whether types of enough length are used for 23 | * vector operations. 24 | * 25 | * Thus, when coordinates are stored as 32-bit values, 64 bits 26 | * should be used in calculations, as 32 bits are not enough to 27 | * hold the result of addition/substraction and especially 28 | * multiplication of two 32 bit values. 29 | */ 30 | 31 | #include 32 | #include 33 | 34 | #include 35 | 36 | int main() { 37 | Vector2i testi(1800000000, 1800000000); 38 | Vector2l testl(1800000000, 1800000000); 39 | 40 | std::cerr << "sizeof(osmint_t) = " << sizeof(osmint_t) << ", sizeof(osmlong_t) = " << sizeof(osmlong_t) << std::endl; 41 | std::cerr << "Vector2i(" << testi.x << "," << testi.y << ").LengthSquare() = " << testi.LengthSquare() << std::endl; 42 | std::cerr << "Vector2l(" << testl.x << "," << testl.y << ").LengthSquare() = " << testl.LengthSquare() << std::endl; 43 | 44 | int result = 0; 45 | 46 | result |= !(testi.LengthSquare() == 6480000000000000000LL); 47 | result |= !(testl.LengthSquare() == 6480000000000000000LL); 48 | 49 | return result; 50 | } 51 | -------------------------------------------------------------------------------- /tests/testing.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2011-2012 Dmitry Marakasov 3 | * All rights reserved. 4 | * 5 | * See https://github.com/AMDmi3/testing.h for updates, bug reports, 6 | * forks etc. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 | * SUCH DAMAGE. 28 | */ 29 | 30 | #ifndef TESTING_H_INCLUDED 31 | #define TESTING_H_INCLUDED 32 | 33 | #include 34 | #include 35 | 36 | // Define NO_TEST_COLOR before including testing.h to disable colors 37 | #ifndef NO_TEST_COLOR 38 | # define TEST_COLOR 39 | #endif 40 | 41 | #ifdef TEST_COLOR 42 | # define PASSED "\e[0;32mPASSED:\e[0m " 43 | # define FAILED "\e[1;31mFAILED:\e[0m " 44 | #else 45 | # define PASSED "PASSED: " 46 | # define FAILED "FAILED: " 47 | #endif 48 | 49 | // Test begin/end 50 | #define BEGIN_TEST() int main() { int num_failing_tests_ = 0; 51 | 52 | #define END_TEST() if (num_failing_tests_ > 0) std::cerr << num_failing_tests_ << " failures" << std::endl; return num_failing_tests_; } 53 | 54 | // Equality checks 55 | #define EXPECT_TRUE(expr) { \ 56 | if (!(expr)) { \ 57 | std::cerr << FAILED #expr << std::endl; \ 58 | ++num_failing_tests_; \ 59 | } else { \ 60 | std::cerr << PASSED #expr << std::endl; \ 61 | } \ 62 | } 63 | 64 | #define EXPECT_STRING(expr, expected) { \ 65 | std::string result = (expr); \ 66 | if (result != expected) { \ 67 | std::cerr << FAILED #expr " returned \"" << result << "\", while expected \"" << expected << "\"" << std::endl; \ 68 | ++num_failing_tests_; \ 69 | } else { \ 70 | std::cerr << PASSED #expr " == \"" << expected << "\"" << std::endl; \ 71 | } \ 72 | } 73 | 74 | #define EXPECT_VALUE(type, expr, expected) { \ 75 | type result = (expr); \ 76 | if (result != expected) { \ 77 | std::cerr << FAILED #expr " returned " << result << ", while expected " << expected << std::endl; \ 78 | ++num_failing_tests_; \ 79 | } else { \ 80 | std::cerr << PASSED #expr " == " << expected << std::endl; \ 81 | } \ 82 | } 83 | 84 | // Range checks 85 | #define EXPECT_VALUE_IN_RANGE(type, expr, from, to) { \ 86 | type result = (expr); \ 87 | if (from <= result && result <= to) { \ 88 | std::cerr << PASSED #expr " returned " << result << ", which is in range [" << from << ", " << to << "] as expected" << std::endl; \ 89 | } else { \ 90 | std::cerr << FAILED #expr " returned " << result << ", which is out of expected range [" << from << ", " << to << "]" << std::endl; \ 91 | ++num_failing_tests_; \ 92 | } \ 93 | } 94 | 95 | // Shortcuts for above; feel free to ask to add more 96 | #define EXPECT_INT(expr, expected) EXPECT_VALUE(int, expr, expected) 97 | #define EXPECT_FLOAT_IN_RANGE(expr, from, to) EXPECT_VALUE_IN_RANGE(float, expr, from, to) 98 | 99 | // Exception checks 100 | #define EXPECT_EXCEPTION(expr, exception) { \ 101 | bool correct_catch = false; \ 102 | try { \ 103 | expr; \ 104 | } catch (exception &e) { \ 105 | correct_catch = true; \ 106 | } catch (...) { \ 107 | } \ 108 | if (correct_catch) { \ 109 | std::cerr << PASSED #expr " has thrown " #exception << " as expected " << std::endl; \ 110 | } else { \ 111 | std::cerr << FAILED #expr " hasn't thrown expected " #exception << std::endl; \ 112 | ++num_failing_tests_; \ 113 | } \ 114 | } 115 | 116 | #define EXPECT_NO_EXCEPTION(expr) { \ 117 | bool had_exception = false; \ 118 | const char* what = NULL; \ 119 | try { \ 120 | expr; \ 121 | } catch (std::exception& e) { \ 122 | had_exception = true; \ 123 | what = e.what(); \ 124 | } catch (...) { \ 125 | had_exception = true; \ 126 | } \ 127 | if (had_exception && what) { \ 128 | std::cerr << FAILED #expr << " has thrown unexpected exception derived from std::exception, what() returned \"" << what << "\"" << std::endl; \ 129 | ++num_failing_tests_; \ 130 | } else if (had_exception) { \ 131 | std::cerr << FAILED #expr << " has thrown unexpected exception not derived from std::exception" << std::endl; \ 132 | ++num_failing_tests_; \ 133 | } else { \ 134 | std::cerr << PASSED #expr << " hasn't thrown any exceptions as expected" << std::endl; \ 135 | } \ 136 | } 137 | 138 | #endif // TESTING_H_INCLUDED 139 | -------------------------------------------------------------------------------- /tiler/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Depends 2 | FIND_PACKAGE(OpenGL REQUIRED) 3 | FIND_PACKAGE(X11 REQUIRED) 4 | FIND_PACKAGE(PNG REQUIRED) 5 | 6 | # Targets 7 | SET(SOURCES 8 | Main.cc 9 | PBuffer.cc 10 | PngWriter.cc 11 | PixelBuffer.cc 12 | ) 13 | 14 | INCLUDE_DIRECTORIES( 15 | ${OPENGL_INCLUDE_DIR} 16 | ${PNG_INCLUDE_DIR} 17 | ${X11_INCLUDE_DIR} 18 | ../libglosm-client 19 | ../libglosm-geomgen 20 | ../libglosm-server 21 | ) 22 | 23 | ADD_EXECUTABLE(glosm-tiler ${SOURCES}) 24 | TARGET_LINK_LIBRARIES(glosm-tiler glosm-client glosm-server glosm-geomgen ${X11_X11_LIB} ${OPENGL_gl_LIBRARY} ${PNG_LIBRARIES}) 25 | 26 | # Installation 27 | INSTALL(TARGETS glosm-tiler RUNTIME DESTINATION ${BINDIR}) 28 | -------------------------------------------------------------------------------- /tiler/PBuffer.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #include 22 | #include 23 | 24 | #include "PBuffer.hh" 25 | 26 | #include "PixelBuffer.hh" 27 | 28 | static bool CheckGLXVersion(Display* display, int screen) { 29 | const char *glxversion; 30 | 31 | glxversion = glXGetClientString(display, GLX_VERSION); 32 | if (strstr(glxversion, "1.3") == NULL && strstr(glxversion, "1.4") == NULL) 33 | return false; 34 | 35 | glxversion = glXQueryServerString(display, screen, GLX_VERSION); 36 | if (strstr(glxversion, "1.3") == NULL && strstr(glxversion, "1.4") == NULL) 37 | return false; 38 | 39 | return true; 40 | } 41 | 42 | PBuffer::PBuffer(int width, int height, int samples) : width_(width), height_(height), display_(NULL), context_(NULL), pbuffer_(None) { 43 | if ((display_ = XOpenDisplay(NULL)) == NULL) 44 | throw PBufferException() << "cannot open default X display"; 45 | 46 | int screen = DefaultScreen(display_); 47 | 48 | if (!CheckGLXVersion(display_, screen)) { 49 | XCloseDisplay(display_); 50 | throw PBufferException() << "GLX 1.3 or 1.4 required, but not available"; 51 | } 52 | 53 | int fbattribs[] = { 54 | GLX_RENDER_TYPE, GLX_RGBA_BIT, 55 | GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT, 56 | GLX_RED_SIZE, 8, 57 | GLX_GREEN_SIZE, 8, 58 | GLX_BLUE_SIZE, 8, 59 | GLX_DEPTH_SIZE, 16, 60 | None, None, 61 | None, None, 62 | None 63 | }; 64 | 65 | if (samples > 1) { 66 | fbattribs[12] = GLX_SAMPLE_BUFFERS; 67 | fbattribs[13] = 1; 68 | fbattribs[14] = GLX_SAMPLES; 69 | fbattribs[15] = samples; 70 | } 71 | 72 | int pbattribs[] = { 73 | GLX_PBUFFER_WIDTH, width_, 74 | GLX_PBUFFER_HEIGHT, height_, 75 | GLX_LARGEST_PBUFFER, False, 76 | GLX_PRESERVED_CONTENTS, False, 77 | None 78 | }; 79 | 80 | int nconfigs; 81 | GLXFBConfig *fbconfigs; 82 | if ((fbconfigs = glXChooseFBConfig(display_, screen, fbattribs, &nconfigs)) == NULL) { 83 | XCloseDisplay(display_); 84 | throw PBufferException() << "glxChooseFBConfig failed"; 85 | } 86 | 87 | if (nconfigs == 0) { 88 | XFree(fbconfigs); 89 | XCloseDisplay(display_); 90 | throw PBufferException() << "no suitable configs returned by glxChooseFBConfig"; 91 | } 92 | 93 | /* Just use first config */ 94 | GLXFBConfig fbconfig = fbconfigs[0]; 95 | XFree(fbconfigs); 96 | 97 | if ((pbuffer_ = glXCreatePbuffer(display_, fbconfig, pbattribs)) == None) { 98 | XCloseDisplay(display_); 99 | throw PBufferException() << "glXCreatePbuffer failed"; 100 | } 101 | 102 | if ((context_ = glXCreateNewContext(display_, fbconfig, GLX_RGBA_TYPE, NULL, True)) == NULL) { 103 | glXDestroyPbuffer(display_, pbuffer_); 104 | XCloseDisplay(display_); 105 | throw PBufferException() << "glXCreateNewContext failed"; 106 | } 107 | 108 | if (!glXMakeCurrent(display_, pbuffer_, context_)) { 109 | glXDestroyContext(display_, context_); 110 | glXDestroyPbuffer(display_, pbuffer_); 111 | XCloseDisplay(display_); 112 | throw PBufferException() << "glXMakeCurrent failed"; 113 | } 114 | } 115 | 116 | PBuffer::~PBuffer() { 117 | if (!glXMakeCurrent(display_, None, NULL)) 118 | warnx("cannot reset GLX context: glXMakeCurrent failed"); 119 | glXDestroyContext(display_, context_); 120 | glXDestroyPbuffer(display_, pbuffer_); 121 | XCloseDisplay(display_); 122 | } 123 | 124 | bool PBuffer::IsDirect() const { 125 | return glXIsDirect(display_, context_); 126 | } 127 | 128 | void PBuffer::GetPixels(PixelBuffer& buffer, int x, int y) { 129 | glPixelStorei(GL_PACK_ALIGNMENT, 1); 130 | glReadPixels(x, y, buffer.GetWidth(), buffer.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, buffer.GetData()); 131 | } 132 | -------------------------------------------------------------------------------- /tiler/PBuffer.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef PBUFFER_HH 22 | #define PBUFFER_HH 23 | 24 | #include 25 | 26 | #include 27 | #include 28 | 29 | #include 30 | 31 | class PBufferException: public Exception { 32 | }; 33 | 34 | class PixelBuffer; 35 | 36 | class PBuffer { 37 | protected: 38 | int width_; 39 | int height_; 40 | 41 | Display* display_; 42 | GLXContext context_; 43 | GLXPbuffer pbuffer_; 44 | 45 | public: 46 | PBuffer(int width, int height, int samples); 47 | ~PBuffer(); 48 | 49 | void GetPixels(PixelBuffer& buffer, int x, int y); 50 | 51 | bool IsDirect() const; 52 | }; 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /tiler/PixelBuffer.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #include "PixelBuffer.hh" 22 | 23 | PixelBuffer::PixelBuffer(int width, int height, int bpp) : width_(width), height_(height), bpp_(bpp), pixels_(width * height * bpp) { 24 | } 25 | 26 | unsigned char* PixelBuffer::GetData() { 27 | return pixels_.data(); 28 | } 29 | 30 | const unsigned char* PixelBuffer::GetRowPointer(int row, int offset) const { 31 | return pixels_.data() + row * width_ * bpp_ + offset * bpp_; 32 | } 33 | 34 | const unsigned char* PixelBuffer::GetReverseRowPointer(int row, int offset) const { 35 | return pixels_.data() + (height_ - 1 - row) * width_ * bpp_ + offset * bpp_; 36 | } 37 | 38 | int PixelBuffer::GetWidth() const { 39 | return width_; 40 | } 41 | 42 | int PixelBuffer::GetHeight() const { 43 | return height_; 44 | } 45 | 46 | int PixelBuffer::GetBPP() const { 47 | return bpp_; 48 | } 49 | -------------------------------------------------------------------------------- /tiler/PixelBuffer.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef PIXELBUFFER_HH 22 | #define PIXELBUFFER_HH 23 | 24 | #include 25 | 26 | class PixelBuffer { 27 | protected: 28 | int width_; 29 | int height_; 30 | int bpp_; 31 | std::vector pixels_; 32 | 33 | public: 34 | PixelBuffer(int width, int height, int bpp); 35 | 36 | unsigned char* GetData(); 37 | 38 | const unsigned char* GetRowPointer(int row, int offset) const; 39 | const unsigned char* GetReverseRowPointer(int row, int offset) const; 40 | 41 | int GetWidth() const; 42 | int GetHeight() const; 43 | int GetBPP() const; 44 | }; 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /tiler/PngWriter.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #include "PngWriter.hh" 22 | 23 | #include "PixelBuffer.hh" 24 | 25 | #include 26 | 27 | #include 28 | 29 | static void png_error_fn(png_struct*, const char* e) { 30 | throw PngWriterException() << "png write error: " << e; 31 | } 32 | 33 | PngWriter::PngWriter(const char* filename, int width, int height, int compression): width_(width), height_(height) { 34 | if ((png_ptr_ = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, png_error_fn, NULL)) == NULL) 35 | throw PngWriterException() << "png_create_write_struct failed"; 36 | 37 | png_set_compression_level(png_ptr_, compression); 38 | 39 | if ((info_ptr_ = png_create_info_struct(png_ptr_)) == NULL) { 40 | png_destroy_write_struct(&png_ptr_, NULL); 41 | throw PngWriterException() << "png_create_info_struct failed"; 42 | } 43 | 44 | if ((file_ = fopen(filename, "wb")) == NULL) { 45 | png_destroy_write_struct(&png_ptr_, &info_ptr_); 46 | throw PngWriterException() << "cannot open output png file: " << filename; 47 | } 48 | 49 | png_init_io(png_ptr_, file_); 50 | 51 | png_set_IHDR(png_ptr_, info_ptr_, width, height, 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); 52 | 53 | png_write_info(png_ptr_, info_ptr_); 54 | } 55 | 56 | PngWriter::~PngWriter() { 57 | png_write_end(png_ptr_, NULL); 58 | png_destroy_write_struct(&png_ptr_, &info_ptr_); 59 | fclose(file_); 60 | } 61 | 62 | void PngWriter::WriteImage(const PixelBuffer& buffer, int x, int y) { 63 | if (x + width_ > buffer.GetWidth()) 64 | throw std::logic_error("output image is wider than input buffer"); 65 | 66 | if (y + height_ > buffer.GetHeight()) 67 | throw std::logic_error("output image is higher than input buffer"); 68 | 69 | png_bytep* row_pointers = new png_bytep[height_]; 70 | 71 | /* ugly cast is hack for png interface which takes non-const png_pytepp */ 72 | for (int i = 0; i < height_; ++i) 73 | row_pointers[i] = const_cast(buffer.GetReverseRowPointer(y + i, x)); 74 | 75 | png_write_image(png_ptr_, row_pointers); 76 | 77 | delete[] row_pointers; 78 | } 79 | -------------------------------------------------------------------------------- /tiler/PngWriter.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef PNGWRITER_HH 22 | #define PNGWRITER_HH 23 | 24 | #include 25 | 26 | #include 27 | 28 | class PixelBuffer; 29 | 30 | class PngWriterException : public Exception { 31 | }; 32 | 33 | class PngWriter { 34 | protected: 35 | FILE* file_; 36 | png_structp png_ptr_; 37 | png_infop info_ptr_; 38 | int width_; 39 | int height_; 40 | 41 | public: 42 | PngWriter(const char* filename, int width, int height, int compression); 43 | virtual ~PngWriter(); 44 | 45 | void WriteImage(const PixelBuffer& buffer, int x, int y); 46 | }; 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /viewer/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Common depends 2 | IF(WITH_TOUCHPAD) 3 | ADD_DEFINITIONS(-DWITH_TOUCHPAD) 4 | ENDIF(WITH_TOUCHPAD) 5 | 6 | IF(BUILD_VIEWER_SDL) 7 | # Depends 8 | FIND_PACKAGE(SDL REQUIRED) 9 | 10 | IF(WITH_GLES OR WITH_GLES2) 11 | FIND_PACKAGE(SDL_gles REQUIRED) 12 | ENDIF(WITH_GLES OR WITH_GLES2) 13 | 14 | # Targets 15 | SET(SOURCES 16 | SDLMain.cc 17 | GlosmViewer.cc 18 | ) 19 | 20 | INCLUDE_DIRECTORIES( 21 | ${OPENGL_INCLUDE_DIR} 22 | ${SDL_INCLUDE_DIR} 23 | ${SDLGLES_INCLUDE_DIR} 24 | ${GL_INCLUDE_DIRS} 25 | ../libglosm-client 26 | ../libglosm-server 27 | ../libglosm-geomgen 28 | ) 29 | 30 | ADD_EXECUTABLE(glosm-viewer-sdl ${SOURCES}) 31 | TARGET_LINK_LIBRARIES(glosm-viewer-sdl 32 | glosm-client 33 | glosm-server 34 | glosm-geomgen 35 | ${SDL_LIBRARY} 36 | ${SDLGLES_LIBRARY} 37 | ${GL_LIBRARIES} 38 | ) 39 | 40 | # Installation 41 | INSTALL(TARGETS glosm-viewer-sdl RUNTIME DESTINATION ${BINDIR}) 42 | ENDIF(BUILD_VIEWER_SDL) 43 | 44 | IF(BUILD_VIEWER_GLUT) 45 | # Depends 46 | FIND_PACKAGE(GLUT REQUIRED) 47 | 48 | # Targets 49 | SET(SOURCES 50 | GLUTMain.cc 51 | GlosmViewer.cc 52 | ) 53 | 54 | INCLUDE_DIRECTORIES( 55 | ${OPENGL_INCLUDE_DIR} 56 | ${GLUT_INCLUDE_DIR} 57 | ${GL_INCLUDE_DIRS} 58 | ../libglosm-client 59 | ../libglosm-server 60 | ../libglosm-geomgen 61 | ) 62 | 63 | ADD_EXECUTABLE(glosm-viewer-glut ${SOURCES}) 64 | TARGET_LINK_LIBRARIES(glosm-viewer-glut 65 | glosm-client 66 | glosm-server 67 | glosm-geomgen 68 | ${GLUT_LIBRARIES} 69 | ${GL_LIBRARIES} 70 | ) 71 | 72 | # Installation 73 | INSTALL(TARGETS glosm-viewer-glut RUNTIME DESTINATION ${BINDIR}) 74 | ENDIF(BUILD_VIEWER_GLUT) 75 | -------------------------------------------------------------------------------- /viewer/GLUTMain.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #include "GlosmViewer.hh" 22 | 23 | #include 24 | #if defined(__APPLE__) 25 | # include 26 | #else 27 | # include 28 | #endif 29 | #if defined(WIN32) 30 | # include 31 | #endif 32 | 33 | #include 34 | 35 | class GlosmViewerImpl : public GlosmViewer { 36 | protected: 37 | virtual void WarpCursor(int x, int y) { 38 | glutWarpPointer(x, y); 39 | } 40 | 41 | virtual void Flip() { 42 | glutSwapBuffers(); 43 | } 44 | 45 | virtual void ShowCursor(bool show) { 46 | glutSetCursor(show ? GLUT_CURSOR_INHERIT : GLUT_CURSOR_NONE); 47 | } 48 | }; 49 | 50 | GlosmViewerImpl app; 51 | 52 | void Display(void) { 53 | app.Render(); 54 | } 55 | 56 | void Reshape(int w, int h) { 57 | app.Resize(w, h); 58 | } 59 | 60 | void Mouse(int x, int y) { 61 | app.MouseMove(x, y); 62 | } 63 | 64 | void Button(int button, int state, int x, int y) { 65 | int b = GlosmViewer::BUTTON_LEFT; 66 | switch (button) { 67 | case GLUT_LEFT_BUTTON: b = GlosmViewer::BUTTON_LEFT; break; 68 | case GLUT_RIGHT_BUTTON: b = GlosmViewer::BUTTON_RIGHT; break; 69 | case GLUT_MIDDLE_BUTTON: b = GlosmViewer::BUTTON_MIDDLE; break; 70 | } 71 | 72 | app.MouseButton(b, state == GLUT_DOWN, x, y); 73 | } 74 | 75 | void SpecialDown(int key, int, int) { 76 | switch (key) { 77 | case GLUT_KEY_UP: app.KeyDown(GlosmViewer::KEY_UP); break; 78 | case GLUT_KEY_DOWN: app.KeyDown(GlosmViewer::KEY_DOWN); break; 79 | case GLUT_KEY_LEFT: app.KeyDown(GlosmViewer::KEY_LEFT); break; 80 | case GLUT_KEY_RIGHT: app.KeyDown(GlosmViewer::KEY_RIGHT); break; 81 | default: break; 82 | } 83 | } 84 | 85 | void SpecialUp(int key, int, int) { 86 | switch (key) { 87 | case GLUT_KEY_UP: app.KeyUp(GlosmViewer::KEY_UP); break; 88 | case GLUT_KEY_DOWN: app.KeyUp(GlosmViewer::KEY_DOWN); break; 89 | case GLUT_KEY_LEFT: app.KeyUp(GlosmViewer::KEY_LEFT); break; 90 | case GLUT_KEY_RIGHT: app.KeyUp(GlosmViewer::KEY_RIGHT); break; 91 | default: break; 92 | } 93 | } 94 | 95 | void KeyDown(unsigned char key, int, int) { 96 | app.KeyDown(key); 97 | } 98 | 99 | void KeyUp(unsigned char key, int, int) { 100 | app.KeyUp(key); 101 | } 102 | 103 | int real_main(int argc, char** argv) { 104 | glutInit(&argc, argv); 105 | 106 | app.Init(argc, argv); 107 | 108 | /* glut init */ 109 | glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA | GLUT_MULTISAMPLE); 110 | glutInitWindowSize(800, 600); 111 | glutCreateWindow("glosm viewer"); 112 | 113 | glutIgnoreKeyRepeat(1); 114 | 115 | glutDisplayFunc(Display); 116 | glutIdleFunc(Display); 117 | glutReshapeFunc(Reshape); 118 | glutMouseFunc(Button); 119 | glutMotionFunc(Mouse); 120 | glutPassiveMotionFunc(Mouse); 121 | glutKeyboardFunc(KeyDown); 122 | glutKeyboardUpFunc(KeyUp); 123 | glutSpecialFunc(SpecialDown); 124 | glutSpecialUpFunc(SpecialUp); 125 | 126 | app.InitGL(); 127 | 128 | /* main loop */ 129 | /* note that this never returns and objects created above 130 | * are never properly destroyed; should dump GLUT ASAP */ 131 | glutMainLoop(); 132 | 133 | return 0; 134 | } 135 | 136 | int main(int argc, char** argv) { 137 | try { 138 | return real_main(argc, argv); 139 | } catch (std::exception &e) { 140 | #if defined(WIN32) 141 | MessageBox(NULL, e.what(), "Fatal error", MB_OK | MB_ICONERROR); 142 | #else 143 | fprintf(stderr, "Fatal error: %s\n", e.what()); 144 | #endif 145 | } catch (...) { 146 | #if defined(WIN32) 147 | MessageBox(NULL, "Unknown exception", "Fatal error", MB_OK | MB_ICONERROR); 148 | #else 149 | fprintf(stderr, "Fatal error: unknown exception\n"); 150 | #endif 151 | } 152 | 153 | return 1; 154 | } 155 | -------------------------------------------------------------------------------- /viewer/GlosmViewer.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2012 Dmitry Marakasov 3 | * 4 | * This file is part of glosm. 5 | * 6 | * glosm is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as 8 | * published by the Free Software Foundation, either version 3 of the 9 | * License, or (at your option) any later version. 10 | * 11 | * glosm is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public 17 | * License along with glosm. If not, see 18 | * . 19 | */ 20 | 21 | #ifndef GLOSMVIEWER_HH 22 | #define GLOSMVIEWER_HH 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | #include 36 | 37 | #include 38 | 39 | class GlosmViewer { 40 | public: 41 | enum Keys { 42 | KEY_UP = 0x100, 43 | KEY_DOWN, 44 | KEY_LEFT, 45 | KEY_RIGHT, 46 | KEY_SHIFT, 47 | KEY_CTRL 48 | }; 49 | 50 | enum Buttons { 51 | BUTTON_LEFT, 52 | BUTTON_MIDDLE, 53 | BUTTON_RIGHT 54 | }; 55 | 56 | protected: 57 | /* flags */ 58 | Projection projection_; 59 | bool no_glew_check_; 60 | 61 | double start_lon_; 62 | double start_lat_; 63 | double start_ele_; 64 | double start_yaw_; 65 | double start_pitch_; 66 | 67 | /* glosm objects */ 68 | std::auto_ptr viewer_; 69 | std::auto_ptr osm_datasource_; 70 | std::auto_ptr gpx_datasource_; 71 | std::auto_ptr heightmap_datasource_; 72 | std::auto_ptr geometry_generator_; 73 | std::auto_ptr ground_layer_; 74 | std::auto_ptr detail_layer_; 75 | std::auto_ptr gpx_layer_; 76 | std::auto_ptr terrain_layer_; 77 | 78 | bool ground_shown_; 79 | bool detail_shown_; 80 | bool gpx_shown_; 81 | bool terrain_shown_; 82 | 83 | int screenw_; 84 | int screenh_; 85 | 86 | struct timeval prevtime_, curtime_, fpstime_; 87 | int nframes_; 88 | 89 | int movementflags_; 90 | float speed_; 91 | bool slow_; 92 | bool fast_; 93 | int lockheight_; 94 | 95 | bool mouse_capture_; 96 | 97 | bool drag_; 98 | Vector2 drag_start_pos_; 99 | float drag_start_pitch_; 100 | float drag_start_yaw_; 101 | 102 | protected: 103 | virtual void WarpCursor(int x, int y) = 0; 104 | virtual void Flip() = 0; 105 | virtual void ShowCursor(bool show) = 0; 106 | 107 | public: 108 | GlosmViewer(); 109 | 110 | virtual void Usage(int status, bool detailed, const char* progname); 111 | virtual void Init(int argc, char** argv); 112 | virtual void InitGL(); 113 | virtual void Render(); 114 | virtual void Resize(int w, int h); 115 | virtual void KeyDown(int key); 116 | virtual void KeyUp(int key); 117 | virtual void MouseMove(int x, int y); 118 | virtual void MouseButton(int button, bool pressed, int x, int y); 119 | }; 120 | 121 | #endif 122 | --------------------------------------------------------------------------------