-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathPosterPrinter.h
197 lines (158 loc) · 7.45 KB
/
PosterPrinter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* * structured - Tools for the Generation and Visualization of Large-scale
* Three-dimensional Reconstructions from Image Data. This software includes
* source code from other projects, which is subject to different licensing,
* see COPYING for details. If this project is used for research see COPYING
* for making the appropriate citations.
* Copyright (C) 2013 Matthew Johnson-Roberson <mattkjr@gmail.com>
*
* This file is part of structured.
*
* structured is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* structured is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with structured. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OSGPOSTER_POSTERPRINTER
#define OSGPOSTER_POSTERPRINTER
#include <osg/Camera>
#include <osg/PagedLOD>
#include <osgUtil/IntersectionVisitor>
/** PosterVisitor: A visitor for adding culling callbacks to newly allocated paged nodes */
class PosterVisitor : public osg::NodeVisitor
{
public:
typedef std::set<std::string> PagedNodeNameSet;
PosterVisitor();
META_NodeVisitor( osgPoster, PosterVisitor );
void insertName( const std::string& name )
{ if ( _pagedNodeNames.insert(name).second ) _needToApplyCount++; }
void eraseName( const std::string& name )
{ if ( _pagedNodeNames.erase(name)>0 ) _needToApplyCount--; }
void clearNames() { _pagedNodeNames.clear(); _needToApplyCount = 0; _appliedCount = 0; }
unsigned int getNumNames() const { return _pagedNodeNames.size(); }
PagedNodeNameSet& getPagedNodeNames() { return _pagedNodeNames; }
const PagedNodeNameSet& getPagedNodeNames() const { return _pagedNodeNames; }
unsigned int getNeedToApplyCount() const { return _needToApplyCount; }
unsigned int getAppliedCount() const { return _appliedCount; }
unsigned int inQueue() const { return _needToApplyCount>_appliedCount ? _needToApplyCount-_appliedCount : 0; }
void setAddingCallbacks( bool b ) { _addingCallbacks = b; }
bool getAddingCallbacks() const { return _addingCallbacks; }
virtual void apply( osg::LOD& node );
virtual void apply( osg::PagedLOD& node );
protected:
bool hasCullCallback( osg::NodeCallback* nc, osg::NodeCallback* target )
{
if ( nc==target ) return true;
else if ( !nc ) return false;
return hasCullCallback( nc->getNestedCallback(), target );
}
PagedNodeNameSet _pagedNodeNames;
unsigned int _appliedCount;
unsigned int _needToApplyCount;
bool _addingCallbacks;
};
/** PosterIntersector: A simple polytope intersector for updating pagedLODs in each image-tile */
class PosterIntersector : public osgUtil::Intersector
{
public:
typedef std::set<std::string> PagedNodeNameSet;
PosterIntersector( const osg::Polytope& polytope );
PosterIntersector( double xMin, double yMin, double xMax, double yMax );
void setPosterVisitor( PosterVisitor* pcv ) { _visitor = pcv; }
PosterVisitor* getPosterVisitor() { return _visitor.get(); }
const PosterVisitor* getPosterVisitor() const { return _visitor.get(); }
virtual Intersector* clone( osgUtil::IntersectionVisitor& iv );
virtual bool containsIntersections()
{ return _visitor.valid()&&_visitor->getNumNames()>0; }
virtual bool enter( const osg::Node& node );
virtual void leave() {}
virtual void reset();
virtual void intersect( osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable );
protected:
osgUtil::IntersectionVisitor* _intersectionVisitor;
osg::ref_ptr<PosterVisitor> _visitor;
PosterIntersector* _parent;
osg::Polytope _polytope;
};
/** PosterPrinter: The implementation class of high-res rendering */
class PosterPrinter : public osg::Referenced
{
public:
typedef std::pair<unsigned int, unsigned int> TilePosition;
typedef std::map< TilePosition, osg::ref_ptr<osg::Image> > TileImages;
PosterPrinter();
/** Set to output each sub-image-tile to disk */
void setOutputTiles( bool b ) { _outputTiles = b; }
bool getOutputTiles() const { return _outputTiles; }
/** Set the output sub-image-tile extension, e.g. bmp */
void setOutputTileExtension( const std::string& ext ) { _outputTileExt = ext; }
const std::string& getOutputTileExtension() const { return _outputTileExt; }
/** Set the output poster name, e.g. output.bmp */
void setOutputPosterName( const std::string& name ) { _outputPosterName = name; }
const std::string& getOutputPosterName() const { return _outputPosterName; }
/** Set the size of each sub-image-tile, e.g. 640x480 */
void setTileSize( int w, int h ) { _tileSize.set(w, h); }
const osg::Vec2& getTileSize() const { return _tileSize; }
/** Set the final size of the high-res poster, e.g. 6400x4800 */
void setPosterSize( int w, int h ) { _posterSize.set(w, h); }
const osg::Vec2& getPosterSize() const { return _posterSize; }
/** Set the capturing camera */
void setCamera( osg::Camera* camera ) { _camera = camera; }
const osg::Camera* getCamera() const { return _camera.get(); }
/** Set the final poster image, should be already allocated */
void setFinalPoster( osg::Image* image ) { _finalPoster = image; }
const osg::Image* getFinalPoster() const { return _finalPoster.get(); }
PosterVisitor* getPosterVisitor() { return _visitor.get(); }
const PosterVisitor* getPosterVisitor() const { return _visitor.get(); }
bool done() const { return !_isRunning && !_isFinishing; }
void init( const osg::Camera* camera );
void init( const osg::Matrixd& view, const osg::Matrixd& proj );
void frame( const osg::FrameStamp* fs, osg::Node* node );
protected:
virtual ~PosterPrinter() {}
bool addCullCallbacks( const osg::FrameStamp* fs, osg::Node* node );
void removeCullCallbacks( osg::Node* node );
void bindCameraToImage( osg::Camera* camera, int row, int col );
void recordImages();
bool _outputTiles;
std::string _outputTileExt;
std::string _outputPosterName;
osg::Vec2 _tileSize;
osg::Vec2 _posterSize;
bool _isRunning;
bool _isFinishing;
unsigned int _lastBindingFrame;
int _tileRows;
int _tileColumns;
int _currentRow;
int _currentColumn;
osg::ref_ptr<PosterIntersector> _intersector;
osg::ref_ptr<PosterVisitor> _visitor;
osg::Matrixd _currentViewMatrix;
osg::Matrixd _currentProjectionMatrix;
osg::ref_ptr<osg::Camera> _camera;
osg::ref_ptr<osg::Image> _finalPoster;
TileImages _images;
};
class CameraVectorPrinter: public PosterPrinter{
public:
void init( std::vector<osg::Matrixd> &views,std::vector<osg::Matrixd> &projs,std::vector<std::string> &fnames);
int _currentIdx;
void bindCameraToImage( osg::Camera* camera, int index);
std::vector<osg::Matrixd> _views;
std::vector<osg::Matrixd> _projs;
std::vector<std::string> _fnames;
void frame( const osg::FrameStamp* fs, osg::Node* node );
typedef std::map< int, osg::ref_ptr<osg::Image> > TileImages1D;
void recordImages();
TileImages1D _images1D;
};
#endif