/* Copyright 2005 Nicholas Bishop * * This file is part of SharpConstruct. * * SharpConstruct 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 2 of the License, or * (at your option) any later version. * * SharpConstruct 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 SharpConstruct; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "Deformations.hh" #include #include using SharpConstruct::GInterface::Deformations; Deformations::Deformations() : flood_( 0, 100 ) {} void Deformations::Initialize( Glib::RefPtr< Gnome::Glade::Xml > glade ) { using sigc::mem_fun; glade_ = glade; // Connect immediates glade_->connect_clicked( "DefSubdivide", mem_fun( *this, &Deformations::Subdivide ) ); glade_->connect_clicked( "DefMirror", mem_fun( *this, &Deformations::on_mirror_ ) ); glade_->connect_clicked( "DefFlipNormals", mem_fun( *this, &Deformations::FlipNormals ) ); glade_->connect_clicked( "DefUnify", mem_fun( *this, &Deformations::Unify ) ); glade_->connect_clicked( "DefToTriangles", mem_fun( *this, &Deformations::ToTriangles ) ); Gtk::Table* pt; glade_->get_widget( "PreviewableTable", pt ); pt->attach( move_, 1, 2, 0, 1 ); pt->attach( scale_, 1, 2, 1, 2 ); pt->attach( deflate_, 1, 2, 2, 3 ); pt->attach( spherize_, 1, 2, 3, 4 ); pt->attach( smooth_, 1, 2, 4, 5 ); pt->attach( flood_, 1, 2, 5, 6 ); std::list< Slider* > sliders; sliders.push_back( &move_ ); sliders.push_back( &scale_ ); sliders.push_back( &deflate_ ); sliders.push_back( &spherize_ ); sliders.push_back( &smooth_ ); sliders.push_back( &flood_ ); move_.SignalContinue().connect( mem_fun( *this, &Deformations::on_move_ ) ); move_.SignalEnd().connect( mem_fun( *this, &Deformations::on_move_ ) ); scale_.SignalContinue().connect( mem_fun( *this, &Deformations::on_scale_ ) ); scale_.SignalEnd().connect( mem_fun( *this, &Deformations::on_scale_ ) ); deflate_.SignalContinue().connect( mem_fun( *this, &Deformations::on_deflate_ ) ); deflate_.SignalEnd().connect( mem_fun( *this, &Deformations::on_deflate_ ) ); spherize_.SignalContinue().connect( mem_fun( *this, &Deformations::on_spherize_ ) ); spherize_.SignalEnd().connect( mem_fun( *this, &Deformations::on_spherize_ ) ); smooth_.SignalContinue().connect( mem_fun( *this, &Deformations::on_smooth_ ) ); smooth_.SignalEnd().connect( mem_fun( *this, &Deformations::on_smooth_ ) ); flood_.SignalContinue().connect( mem_fun( *this, &Deformations::on_flood_ ) ); flood_.SignalEnd().connect( mem_fun( *this, &Deformations::on_flood_ ) ); for( std::list< Slider* >::iterator i = sliders.begin(); i != sliders.end(); ++i ) { ( *i )->SignalStart().connect( mem_fun( *this, &Deformations::before_deformation_ ) ); ( *i )->SignalEnd().connect( mem_fun( *this, &Deformations::after_deformation_ ) ); } } bool Deformations::x_() const { Gtk::ToggleButton* tb; glade_->get_widget( "DefX", tb ); return tb->get_active(); } bool Deformations::y_() const { Gtk::ToggleButton* tb; glade_->get_widget( "DefY", tb ); return tb->get_active(); } bool Deformations::z_() const { Gtk::ToggleButton* tb; glade_->get_widget( "DefZ", tb ); return tb->get_active(); } bool Deformations::preview_() const { Gtk::CheckButton* cb; glade_->get_widget( "PreviewDeformations", cb ); return cb->get_active(); } void Deformations::Subdivide() { Gtk::CheckButton* cb; glade_->get_widget( "DefSubdivideSmooth", cb ); MeshHistory::Instance().AddMesh( true, true, true ); MeshHistory::Instance().GetCurrentMesh().Subdivide( cb->get_active() ); MeshHistory::Instance().GetCurrentMesh().RecalculateAllNormals(); } void Deformations::on_mirror_() { MeshHistory::Instance().AddMesh( true, false, true ); MeshHistory::Instance().GetCurrentMesh().Mirror( x_(), y_(), z_() ); } void Deformations::FlipNormals() { MeshHistory::Instance().AddMesh( false, false, true ); MeshHistory::Instance().GetCurrentMesh().FlipNormals(); } void Deformations::Unify() { MeshHistory::Instance().AddMesh( true, true, true ); MeshHistory::Instance().GetCurrentMesh().Unify(); } void Deformations::ToTriangles() { MeshHistory::Instance().AddMesh( true, true, true ); MeshHistory::Instance().GetCurrentMesh().ToTriangles(); } void Deformations::before_deformation_( const double ) { if( preview_() ) { mesh_copy_.SetVertices( new MeshHistory::PartialMesh::PartialVertexData ); mesh_copy_.SetColors( new MeshHistory::PartialMesh::PartialColorData ); *mesh_copy_.Vertices() = *MeshHistory::Instance().CurrentComposite().Vertices(); *mesh_copy_.Colors() = *MeshHistory::Instance().CurrentComposite().Colors(); } } void Deformations::revert_to_copy_() { *MeshHistory::Instance().CurrentComposite().Vertices() = *mesh_copy_.Vertices(); *MeshHistory::Instance().CurrentComposite().Colors() = *mesh_copy_.Colors(); } void Deformations::on_move_( const double v ) { if( preview_() || !move_.InSlide() ) { if( preview_() ) revert_to_copy_(); else MeshHistory::Instance().AddMesh( true, false, false ); float scaled_value = v / 100; scaled_value *= MeshHistory::Instance().GetCurrentMesh().Diameter(); MeshHistory::Instance().GetCurrentMesh().Move ( scaled_value * x_(), scaled_value * y_(), scaled_value * z_() ); } } void Deformations::on_scale_( const double v ) { if( preview_() || !scale_.InSlide() ) { if( preview_() ) revert_to_copy_(); else MeshHistory::Instance().AddMesh( true, false, false ); float clipped_value = v / 50; if( clipped_value > 0 ) clipped_value++; else clipped_value--; MeshHistory::Instance().GetCurrentMesh().Resize( clipped_value * x_(), clipped_value * y_(), clipped_value * z_() ); } } void Deformations::on_deflate_( const double v ) { if( preview_() || !deflate_.InSlide() ) { if( preview_() ) revert_to_copy_(); else MeshHistory::Instance().AddMesh( true, false, false ); MeshHistory::Instance().GetCurrentMesh().Deflate( v / 200 ); } } void Deformations::on_spherize_( const double v ) { if( preview_() || !spherize_.InSlide() ) { if( preview_() ) revert_to_copy_(); else MeshHistory::Instance().AddMesh( true, false, false ); MeshHistory::Instance().GetCurrentMesh().Spherize( v / 100 ); } } void Deformations::on_smooth_( const double v ) { if( preview_() || !smooth_.InSlide() ) { if( preview_() ) revert_to_copy_(); else MeshHistory::Instance().AddMesh( true, false, false ); MeshHistory::Instance().GetCurrentMesh().Smooth( v / 100 ); } } void Deformations::on_flood_( const double v ) { if( preview_() || !flood_.InSlide() ) { if( preview_() ) revert_to_copy_(); else MeshHistory::Instance().AddMesh( false, true, false ); Gtk::ColorButton* cb; glade_->get_widget( "DefColor", cb ); const Gdk::Color gcol( cb->get_color() ); Color color( gcol.get_red_p(), gcol.get_green_p(), gcol.get_blue_p() );; MeshHistory::Instance().GetCurrentMesh().Flood( color, v / 100 ); } } void Deformations::after_deformation_( const double ) { if( preview_() ) { MeshHistory::PartialMesh::PartialVertexData* pvd = MeshHistory::Instance().CurrentComposite().Vertices(); MeshHistory::PartialMesh::PartialColorData* pcd = MeshHistory::Instance().CurrentComposite().Colors(); MeshHistory::Instance().AddMesh( true, true, false ); *pvd = *mesh_copy_.Vertices(); *pcd = *mesh_copy_.Colors(); } }