Skip to content
Permalink
Browse files

Place 3D models on point layer

  • Loading branch information
pka committed Dec 31, 2010
1 parent 764d002 commit b49316aa021e690942a81138dd21a7c6cdfed9ff
@@ -27,6 +27,9 @@
#include <qgslogger.h>
#include <qgsapplication.h>
#include <qgsmapcanvas.h>
#include <qgsfeature.h>
#include <qgsgeometry.h>
#include <qgspoint.h>

#include <QAction>
#include <QToolBar>
@@ -169,6 +172,7 @@ void GlobePlugin::run()

void GlobePlugin::settings()
{
mSettingsDialog.updatePointLayers();
if( mSettingsDialog.exec() )
{
//viewer stereo settings set by mSettingsDialog and stored in QSettings
@@ -220,45 +224,24 @@ void GlobePlugin::setupMap()
mElevationManager->setMaxTilesToCache( 50 );

mObjectPlacer = new osgEarthUtil::ObjectPlacer( mMapNode );

#if 0
// model placement test

// create simple tree model from primitives
osg::TessellationHints* hints = new osg::TessellationHints();
hints->setDetailRatio( 0.1 );

osg::Cylinder* cylinder = new osg::Cylinder( osg::Vec3( 0 , 0, 5 ), 0.5, 10 );
osg::ShapeDrawable* cylinderDrawable = new osg::ShapeDrawable( cylinder, hints );
cylinderDrawable->setColor( osg::Vec4( 0.5, 0.25, 0.125, 1.0 ) );
osg::Geode* cylinderGeode = new osg::Geode();
cylinderGeode->addDrawable( cylinderDrawable );

osg::Cone* cone = new osg::Cone( osg::Vec3( 0 , 0, 10 ), 4, 10 );
osg::ShapeDrawable* coneDrawable = new osg::ShapeDrawable( cone, hints );
coneDrawable->setColor( osg::Vec4( 0.0, 0.5, 0.0, 1.0 ) );
osg::Geode* coneGeode = new osg::Geode();
coneGeode->addDrawable( coneDrawable );

osg::Group* model = new osg::Group();
model->addChild( cylinderGeode );
model->addChild( coneGeode );

// place models on jittered grid
srand( 23 );
double lat = 47.1786;
double lon = 10.111;
double gridSize = 0.001;
for( int i = 0; i < 10; i++ )
if ( mSettingsDialog.modelLayer() && !mSettingsDialog.modelPath().isEmpty() )
{
for( int j = 0; j < 10; j++ )
osg::Node* model = osgDB::readNodeFile( mSettingsDialog.modelPath().toStdString() );
if ( model )
{
double dx = gridSize * ( rand() / (( double ) RAND_MAX + 1.0 ) - 0.5 );
double dy = gridSize * ( rand() / (( double ) RAND_MAX + 1.0 ) - 0.5 );
placeNode( model, lat + i * gridSize + dx, lon + j * gridSize + dy );
QgsVectorLayer* layer = mSettingsDialog.modelLayer();
QgsAttributeList fetchAttributes;
layer->select( fetchAttributes ); //TODO: select only visible features
QgsFeature feature;
while ( layer->nextFeature( feature ) )
{
QgsPoint point = feature.geometry()->asPoint();
QgsDebugMsg( "placeNode" + point.toString() );
placeNode( model, point.y(), point.x() );
}
}
}
#endif

}

void GlobePlugin::projectReady()
@@ -21,6 +21,8 @@

#include <qgsapplication.h>
#include <qgsproject.h>
#include <qgsmaplayerregistry.h>
#include <qgsmaplayer.h>
#include <qgslogger.h>
#include <qgscontexthelp.h>
#include <QtAlgorithms>
@@ -55,7 +57,7 @@ QgsGlobePluginDialog::~QgsGlobePluginDialog()
{
}

QString QgsGlobePluginDialog::openFile()
QString QgsGlobePluginDialog::openRasterFile()
{
//see http://www.gdal.org/formats_list.html
const char* filter = "GDAL files (*.dem *.tif *.tiff *.jpg *.jpeg *.asc) \
@@ -161,7 +163,7 @@ void QgsGlobePluginDialog::on_elevationCombo_currentIndexChanged(QString type)

void QgsGlobePluginDialog::on_elevationBrowse_clicked()
{
QString newPath = openFile();
QString newPath = openRasterFile();
if ( ! newPath.isEmpty() )
{
elevationPath->setText( newPath );
@@ -326,6 +328,63 @@ void QgsGlobePluginDialog::saveElevationDatasources()
}
//END ELEVATION

//MODEL
QList<QgsVectorLayer*> QgsGlobePluginDialog::pointLayers()
{
QList<QgsVectorLayer*> list;
QMap< QString, QgsMapLayer *> layers = QgsMapLayerRegistry::instance()->mapLayers();
QMapIterator<QString, QgsMapLayer *> it(layers);
while (it.hasNext())
{
it.next();
QgsMapLayer* layer = it.value();
if (layer->type() == QgsMapLayer::VectorLayer)
{
QgsVectorLayer* vectorLayer = static_cast<QgsVectorLayer*>(layer);
if ( vectorLayer->geometryType() == QGis::Point )
{
list.append( vectorLayer );
}
}
}
return list;
}

void QgsGlobePluginDialog::updatePointLayers()
{
modelLayerCombo->clear();
QList<QgsVectorLayer*> layers = pointLayers();
QListIterator<QgsVectorLayer*> it(layers);
while (it.hasNext())
{
QgsVectorLayer* layer = it.next();
modelLayerCombo->addItem( layer->name() );
}
}

QgsVectorLayer* QgsGlobePluginDialog::modelLayer()
{
QList<QgsVectorLayer*> layers = pointLayers();
return ( modelLayerCombo->currentIndex() == -1 ) ? NULL : layers.at( modelLayerCombo->currentIndex() );
}

void QgsGlobePluginDialog::on_modelBrowse_clicked()
{
//see http://www.openscenegraph.org/projects/osg/wiki/Support/UserGuides/Plugins
const char* filter = "Model files (*.3dc *.asc *.3ds *.ac *.bsp *.dae *.dw *.dxf *.fbx *.gem *.geo *.iv *.wrl *.ive *.logo *.lwo *.lw *.geo *.lws *.md2 *.obj *.ogr *.flt *.osg *.shp *.stl *.sta *.wrl *.x) \
;;All files (*.*)";
QString path = QFileDialog::getOpenFileName( this,
tr( "Open 3D model file" ),
QDir::homePath (),
tr( filter ) );

if ( ! path.isEmpty() )
{
modelPathLineEdit->setText( path );
}
}
//END MODEL

//STEREO
void QgsGlobePluginDialog::on_resetStereoDefaults_clicked()
{
@@ -25,6 +25,7 @@
#include "qgsosgviewer.h"
#include "qgscontexthelp.h"
#include <qgsproject.h>
#include <qgsvectorlayer.h>

class QgsGlobePluginDialog:public QDialog, private Ui::QgsGlobePluginDialogGuiBase
{
@@ -35,19 +36,22 @@ class QgsGlobePluginDialog:public QDialog, private Ui::QgsGlobePluginDialogGuiBa
~QgsGlobePluginDialog();
void readElevationDatasources();
QTableWidget *elevationDatasources() { return elevationDatasourcesWidget; }

void updatePointLayers();
QgsVectorLayer* modelLayer();
QString modelPath() { return modelPathLineEdit->text(); }
private:
QgsOsgViewer* mViewer;
QSettings settings;
private:
QString openFile();
QString openRasterFile();
void updateStereoDialog();
bool validateResource( QString type, QString uri, QString& error);
void saveElevationDatasources();
void moveRow( QTableWidget* widget, bool up);
QList<QTableWidgetItem*> takeRow( QTableWidget* widget, int row);
void setRow( QTableWidget* widget, int row, const QList< QTableWidgetItem* >& rowItems);
void showMessageBox( QString text);
QList<QgsVectorLayer*> pointLayers();
//! Set osg/DisplaySettings
void setStereoConfig();
//! Init dialog from settings using defaults from osg/DisplaySettings
@@ -73,6 +77,9 @@ class QgsGlobePluginDialog:public QDialog, private Ui::QgsGlobePluginDialogGuiBa
void on_splitStereoVerticalEyeMapping_currentIndexChanged(int value);
void on_resetStereoDefaults_clicked();

//MODEL
void on_modelBrowse_clicked();

//ELEVATION
void on_elevationCombo_currentIndexChanged(QString type);
void on_elevationBrowse_clicked();
@@ -32,8 +32,8 @@
<iconset>
<normaloff>../../.designer/backup</normaloff>../../.designer/backup</iconset>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0">
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>0</number>
@@ -50,6 +50,9 @@
<property name="text">
<string>Type</string>
</property>
<property name="buddy">
<cstring>elevationCombo</cstring>
</property>
</widget>
</item>
<item>
@@ -89,6 +92,9 @@
<property name="text">
<string>URL/File</string>
</property>
<property name="buddy">
<cstring>elevationPath</cstring>
</property>
</widget>
</item>
<item>
@@ -113,7 +119,7 @@
</sizepolicy>
</property>
<property name="currentIndex">
<number>0</number>
<number>1</number>
</property>
<widget class="QWidget" name="elevationIsLocal">
<layout class="QGridLayout" name="gridLayout_5">
@@ -295,6 +301,78 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="model">
<attribute name="title">
<string>Model</string>
</attribute>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QLabel" name="label_11">
<property name="text">
<string>Point Layer</string>
</property>
<property name="buddy">
<cstring>modelLayerCombo</cstring>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_14">
<property name="text">
<string>3D Model</string>
</property>
<property name="buddy">
<cstring>modelPathLineEdit</cstring>
</property>
</widget>
</item>
<item row="1" column="3">
<widget class="QLineEdit" name="modelPathLineEdit">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="1" column="4">
<widget class="QPushButton" name="modelBrowse">
<property name="enabled">
<bool>true</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>27</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="0" column="3">
<widget class="QComboBox" name="modelLayerCombo">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="stereo">
<attribute name="title">
<string>Stereo</string>
@@ -515,7 +593,7 @@
</widget>
</widget>
</item>
<item>
<item row="1" column="0">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
@@ -547,8 +625,8 @@
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>442</x>
<y>460</y>
<x>446</x>
<y>529</y>
</hint>
<hint type="destinationlabel">
<x>481</x>
@@ -563,8 +641,8 @@
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>383</x>
<y>460</y>
<x>387</x>
<y>529</y>
</hint>
<hint type="destinationlabel">
<x>281</x>

0 comments on commit b49316a

Please sign in to comment.
You can’t perform that action at this time.