36#include <wx/mstream.h>
56template <glm::length_t L, glm::length_t C,
class T, glm::qualifier Q>
57bool equals( glm::mat<L, C, T, Q>
const& aFirst, glm::mat<L, C, T, Q>
const& aSecond,
58 T aEpsilon =
static_cast<T
>( FLT_EPSILON * 10 ) )
60 T
const* first = glm::value_ptr( aFirst );
61 T
const* second = glm::value_ptr( aSecond );
63 for( glm::length_t j = 0; j < L *
C; ++j )
65 if( !
equals( first[j], second[j], aEpsilon ) )
76 NAV_3D( false, false ), m_canvas( aCanvas ), m_capIsMoving( false )
80 PutProfileHint(
"KiCAD 3D" );
82 EnableNavigation(
true );
83 PutFrameTimingSource( TimingSource::SpaceMouse );
91 EnableNavigation(
false );
97 wxLogTrace(
m_logTrace, wxT(
"NL_3D_VIEWER_PLUGIN_IMPL::SetFocus %d" ), aFocus );
98 NAV_3D::Write( navlib::focus_k, aFocus );
115 using TDx::SpaceMouse::CCategory;
117 CATEGORY_STORE::iterator parent_iter = aCategoryStore.begin();
118 std::string::size_type pos = aCategoryPath.find_last_of(
'.' );
120 if( pos != std::string::npos )
122 std::string parentPath = aCategoryPath.substr( 0, pos );
123 parent_iter = aCategoryStore.find( parentPath );
125 if( parent_iter == aCategoryStore.end() )
127 parent_iter =
add_category( parentPath, aCategoryStore );
131 std::string
name = aCategoryPath.substr( pos + 1 );
132 std::unique_ptr<CCategory> categoryNode =
133 std::make_unique<CCategory>( aCategoryPath.c_str(),
name.c_str() );
135 CATEGORY_STORE::iterator iter = aCategoryStore.insert(
136 aCategoryStore.end(), CATEGORY_STORE::value_type( aCategoryPath, categoryNode.get() ) );
138 parent_iter->second->push_back( std::move( categoryNode ) );
145 wxLogTrace(
m_logTrace, wxT(
"NL_3D_VIEWER_PLUGIN_IMPL::exportCommandsAndImages" ) );
149 if( actions.size() == 0 )
154 using TDx::SpaceMouse::CCommand;
155 using TDx::SpaceMouse::CCommandSet;
158 CCommandSet commandSet(
"EDA_3D_CANVAS",
"3D Viewer" );
161 NAV_3D::PutActiveCommands( commandSet.GetId() );
166 std::vector<TDx::CImage> vImages;
169 categoryStore.insert( categoryStore.end(), CATEGORY_STORE::value_type(
".", &commandSet ) );
171 std::list<TOOL_ACTION*>::const_iterator it;
173 for( it = actions.begin(); it != actions.end(); ++it )
176 std::string label = action->
GetLabel().ToStdString();
186 if(
name.rfind(
"pcbnew.", 0 ) == 0 )
192 CATEGORY_STORE::iterator iter = categoryStore.find( strCategory );
194 if( iter == categoryStore.end() )
196 iter =
add_category( std::move( strCategory ), categoryStore );
202 wxMemoryOutputStream imageStream;
207 image.SaveFile( imageStream, wxBitmapType::wxBITMAP_TYPE_PNG );
210 if( imageStream.GetSize() )
212 wxStreamBuffer* streamBuffer = imageStream.GetOutputStreamBuffer();
213 TDx::CImage tdxImage = TDx::CImage::FromData(
"", 0,
name.c_str() );
214 tdxImage.AssignImage( std::string(
reinterpret_cast<const char*
>(
215 streamBuffer->GetBufferStart() ),
216 streamBuffer->GetBufferSize() ),
220 vImages.push_back( std::move( tdxImage ) );
224 wxLogTrace(
m_logTrace, wxT(
"Inserting command: %s, description: %s, in category: %s" ),
225 name, description, iter->first );
227 iter->second->push_back(
228 CCommand( std::move(
name ), std::move( label ), std::move( description ) ) );
231 NAV_3D::AddCommandSet( commandSet );
232 NAV_3D::AddImages( vImages );
242 std::copy_n( glm::value_ptr( glm::inverse(
m_cameraMatrix ) ), 16, matrix.m );
253 position = { origin.x, origin.y, origin.z };
263 return navlib::make_result_code( navlib::navlib_errc::invalid_operation );
268 double half_width = f.
fw / 2.;
269 double half_height = f.
fh / 2.;
270 extents = { -half_width, -half_height, f.
nearD, half_width, half_height, f.
farD };
280 aFov = glm::radians( f.
angle );
289 return navlib::make_result_code( navlib::navlib_errc::invalid_operation );
293 double half_width = f.
nw / 2.;
294 double half_height = f.
nh / 2.;
295 aFrustum = { -half_width, half_width, -half_height, half_height, f.
nearD, f.
farD };
313 glm::mat4 cam, viewMatrix;
314 std::copy_n( aCameraMatrix.m, 16, glm::value_ptr( cam ) );
315 viewMatrix = glm::inverse( cam );
328 glm::vec4 lookat_new = viewMatrix * lookat;
329 glm::vec4 lookat_old = camera * lookat;
331 viewMatrix[3].z += lookat_old.z - lookat_new.z;
346 return navlib::make_result_code( navlib::navlib_errc::error );
360 return navlib::make_result_code( navlib::navlib_errc::error );
371 float factor = f.
nw / ( extents.max_x - extents.min_x );
384 return navlib::make_result_code( navlib::navlib_errc::error );
393 return navlib::make_result_code( navlib::navlib_errc::function_not_supported );
399 return navlib::make_result_code( navlib::navlib_errc::permission_denied );
408 extents = { min.x, min.y, min.z, max.x, max.y, max.z };
416 return navlib::make_result_code( navlib::navlib_errc::no_data_available );
422 return navlib::make_result_code( navlib::navlib_errc::no_data_available );
436 return navlib::make_result_code( navlib::navlib_errc::invalid_operation );
444 position = { lap.x, lap.y, lap.z };
463 m_canvas->Set3dmousePivotPos( pivotPos );
476 visible =
m_canvas->GetRender3dmousePivot();
484 m_canvas->SetRender3dmousePivot( visible );
504 vec = mouseRay.
at( hit );
505 position = { vec.x, vec.y, vec.z };
509 return navlib::make_result_code( navlib::navlib_errc::no_data_available );
515 return navlib::make_result_code( navlib::navlib_errc::function_not_supported );
537 return navlib::make_result_code( navlib::navlib_errc::function_not_supported );
543 if( commandId.empty() )
551 for( std::list<TOOL_ACTION*>::const_iterator it = actions.begin(); it != actions.end(); it++ )
554 std::string nm = action->
GetName();
556 if( commandId == nm )
562 if( context !=
nullptr )
564 wxWindow* parent =
m_canvas->GetParent();
569 if( parent->IsEnabled() )
573 if( tool_manager ==
nullptr )
575 return navlib::make_result_code( navlib::navlib_errc::invalid_operation );
581 bool runAction =
true;
586 runAction = aCond->enableCondition( sel );
591 tool_manager->
RunAction( *context,
true );
597 return navlib::make_result_code( navlib::navlib_errc::invalid_operation );
627 wxLogTrace(
m_logTrace, wxT(
"End of transaction" ) );
636 return navlib::make_result_code( navlib::navlib_errc::function_not_supported );
642 matrix = { 1, 0, 0, 0, 0, 0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1 };
650 matrix = { 1, 0, 0, 0, 0, 0, -1, 0, 0, 1, 0, 0, 0, 0, 0, 1 };
wxBitmap KiBitmap(BITMAPS aBitmap, int aHeightTag)
Construct a wxBitmap from an image identifier Returns the image from the active theme if the image ha...
const ACTION_CONDITIONS * GetCondition(const TOOL_ACTION &aAction) const
Get the conditions to use for a specific tool action.
static std::list< TOOL_ACTION * > & GetActionList()
Return list of TOOL_ACTIONs.
const BBOX_3D & GetBBox() const noexcept
Get the board outling bounding box.
const CAMERA_FRUSTUM & GetFrustum() const
PROJECTION_TYPE GetProjection()
const SFVEC3F & GetLookAtPos() const
void MakeRayAtCurrentMousePosition(SFVEC3F &aOutOrigin, SFVEC3F &aOutDirection) const
Make a ray based on the latest mouse position.
const glm::mat4 & GetViewMatrix() const
void SetLookAtPos_T1(const SFVEC3F &aLookAtPos)
void SetViewMatrix(glm::mat4 aViewMatrix)
Set the affine matrix to be applied to a transformation camera.
void Update()
Update the camera.
Implement a canvas based on a wxGLCanvas.
void Request_refresh(bool aRedrawImmediately=true)
Schedule a refresh update of the canvas.
const BOARD_ADAPTER & GetBoardAdapter() const
Get information used to display 3D board.
CAMERA * GetCamera()
Get the canvas camera.
long GetViewExtents(navlib::box_t &aExtents) const override
long GetCameraMatrix(navlib::matrix_t &aMatrix) const override
long SetHitLookFrom(const navlib::point_t &aPosition) override
long SetActiveCommand(std::string aCommandId) override
long GetPivotPosition(navlib::point_t &aPosition) const override
long GetModelExtents(navlib::box_t &aExtents) const override
long GetPointerPosition(navlib::point_t &aPosition) const override
long SetPivotVisible(bool aVisible) override
long SetTransaction(long aValue) override
long SetViewFOV(double aFov) override
long SetMotionFlag(bool aValue) override
long SetCameraMatrix(const navlib::matrix_t &aMatrix) override
long GetHitLookAt(navlib::point_t &aPosition) const override
long SetHitAperture(double aAperture) override
long SetPivotPosition(const navlib::point_t &aPosition) override
long SetHitDirection(const navlib::vector_t &aDirection) override
long GetSelectionTransform(navlib::matrix_t &aTransform) const override
long SetSelectionTransform(const navlib::matrix_t &aMatrix) override
long SetSettingsChanged(long aChangeNumber) override
virtual ~NL_3D_VIEWER_PLUGIN_IMPL()
long SetHitSelectionOnly(bool aSelectionOnly) override
long GetIsSelectionEmpty(navlib::bool_t &aEmpty) const override
NL_3D_VIEWER_PLUGIN_IMPL(EDA_3D_CANVAS *aCanvas)
Initializes a new instance of the NL_3DVIEWER_PLUGIN.
long GetSelectionExtents(navlib::box_t &aExtents) const override
long GetCoordinateSystem(navlib::matrix_t &aMatrix) const override
long IsUserPivot(navlib::bool_t &aUserPivot) const override
long GetViewFOV(double &aFov) const override
long GetPivotVisible(navlib::bool_t &aVisible) const override
long GetFrontView(navlib::matrix_t &aMatrix) const override
long GetViewFrustum(navlib::frustum_t &aFrustum) const override
long GetIsViewPerspective(navlib::bool_t &aPerspective) const override
long SetViewFrustum(const navlib::frustum_t &aFrustum) override
long SetViewExtents(const navlib::box_t &aExtents) override
void exportCommandsAndImages()
Export the invocable actions and images to the 3Dconnexion UI.
void SetFocus(bool aFocus=true)
Set the connection to the 3Dconnexion driver to the focus state so that 3DMouse data is routed here.
long GetIsViewRotatable(navlib::bool_t &isRotatable) const override
long SetCameraTarget(const navlib::point_t &aPosition) override
Base PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
static bool empty(const wxTextEntryBase *aCtrl)
static const wxChar * m_logTrace
Trace mask used to enable or disable the trace output of this class.
bool equals(glm::mat< L, C, T, Q > const &aFirst, glm::mat< L, C, T, Q > const &aSecond, T aEpsilon=static_cast< T >(FLT_EPSILON *10))
Template to compare two glm::mat<T> values for equality within a required epsilon.
std::map< std::string, TDx::CCommandTreeNode * > CATEGORY_STORE
CATEGORY_STORE::iterator add_category(std::string aCategoryPath, CATEGORY_STORE &aCategoryStore)
Add a category to the store.
Declaration of the NL_3D_VIEWER_PLUGIN_IMPL class.
TDx::SpaceMouse::Navigation3D::CNavigation3D NAV_3D
Functors that can be used to figure out how the action controls should be displayed in the UI and if ...
bool Intersect(const RAY &aRay, float *t) const
const SFVEC3F & Min() const
Return the minimum vertex pointer.
const SFVEC3F & Max() const
Return the maximum vertex pointer.
Frustum is a implementation based on a tutorial by http://www.lighthouse3d.com/tutorials/view-frustum...
void Init(const SFVEC3F &o, const SFVEC3F &d)
SFVEC3F at(float t) const
Declaration for a track ball camera.