-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Moved ScreenToPlane and ScreenToScene from ign-gui to ign-rendering #363
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
efa50b8
Moved ScreenToPlane and ScreenToScene from ign-gui to ign-rendering
ahcorde 7524873
added plane offset
ahcorde c3f358e
Added new signature
ahcorde 4f85db9
Fix doxygen
scpeters 0a8537a
Add const version of RayQueryResult bool operator
scpeters daf732a
Improvements to Utils_TEST
scpeters 60107ba
Merge branch 'ahcorde/feature/clickto' of https://github.com/ignition…
ahcorde 7d864e1
Test ScreenToPlane with non-zero offset
scpeters File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
/* * Copyright (C) 2021 Open Source Robotics Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
#include <gtest/gtest.h> | ||
|
||
#include <ignition/common/Console.hh> | ||
|
||
#include "ignition/rendering/Camera.hh" | ||
#include "ignition/rendering/RayQuery.hh" | ||
#include "ignition/rendering/RenderEngine.hh" | ||
#include "ignition/rendering/RenderingIface.hh" | ||
#include "ignition/rendering/Scene.hh" | ||
#include "ignition/rendering/Utils.hh" | ||
#include "ignition/rendering/Visual.hh" | ||
|
||
#include "test_config.h" // NOLINT(build/include) | ||
|
||
using namespace ignition; | ||
using namespace rendering; | ||
|
||
class UtilTest : public testing::Test, | ||
public testing::WithParamInterface<const char *> | ||
{ | ||
// Documentation inherited | ||
public: void SetUp() override | ||
{ | ||
ignition::common::Console::SetVerbosity(4); | ||
} | ||
|
||
public: void ClickToScene(const std::string &_renderEngine); | ||
}; | ||
|
||
void UtilTest::ClickToScene(const std::string &_renderEngine) | ||
{ | ||
RenderEngine *engine = rendering::engine(_renderEngine); | ||
if (!engine) | ||
{ | ||
igndbg << "Engine '" << _renderEngine | ||
<< "' is not supported" << std::endl; | ||
return; | ||
} | ||
ScenePtr scene = engine->CreateScene("scene"); | ||
|
||
CameraPtr camera(scene->CreateCamera()); | ||
EXPECT_TRUE(camera != nullptr); | ||
|
||
camera->SetLocalPosition(0.0, 0.0, 15); | ||
camera->SetLocalRotation(0.0, IGN_PI / 2, 0.0); | ||
|
||
unsigned int width = 640u; | ||
unsigned int height = 480u; | ||
camera->SetImageWidth(width); | ||
camera->SetImageHeight(height); | ||
|
||
const int halfWidth = static_cast<int>(width / 2); | ||
const int halfHeight = static_cast<int>(height / 2); | ||
const ignition::math::Vector2i centerClick(halfWidth, halfHeight); | ||
|
||
RayQueryPtr rayQuery = scene->CreateRayQuery(); | ||
EXPECT_TRUE(rayQuery != nullptr); | ||
|
||
// ScreenToPlane | ||
math::Vector3d result = ScreenToPlane(centerClick, camera, rayQuery); | ||
|
||
EXPECT_NEAR(0.0, result.Z(), 1e-10); | ||
EXPECT_NEAR(0.0, result.X(), 2e-6); | ||
EXPECT_NEAR(0.0, result.Y(), 2e-6); | ||
|
||
// call with non-zero plane offset | ||
result = ScreenToPlane(centerClick, camera, rayQuery, 5.0); | ||
|
||
EXPECT_NEAR(5.0, result.Z(), 1e-10); | ||
EXPECT_NEAR(0.0, result.X(), 2e-6); | ||
EXPECT_NEAR(0.0, result.Y(), 2e-6); | ||
|
||
// ScreenToScene | ||
// API without RayQueryResult and default max distance | ||
result = ScreenToScene(centerClick, camera, rayQuery); | ||
|
||
// No objects currently in the scene, so return a point max distance in | ||
// front of camera | ||
// The default max distance is 10 meters away | ||
EXPECT_NEAR(5.0 - camera->NearClipPlane(), result.Z(), 4e-6); | ||
EXPECT_NEAR(0.0, result.X(), 2e-6); | ||
EXPECT_NEAR(0.0, result.Y(), 2e-6); | ||
|
||
// Try with different max distance | ||
RayQueryResult rayResult; | ||
result = ScreenToScene(centerClick, camera, rayQuery, rayResult, 20.0); | ||
|
||
EXPECT_NEAR(-5.0 - camera->NearClipPlane(), result.Z(), 4e-6); | ||
EXPECT_NEAR(0.0, result.X(), 4e-6); | ||
EXPECT_NEAR(0.0, result.Y(), 4e-6); | ||
EXPECT_FALSE(rayResult); | ||
EXPECT_EQ(0u, rayResult.objectId); | ||
|
||
VisualPtr root = scene->RootVisual(); | ||
|
||
// create box visual to collide with the ray | ||
VisualPtr box = scene->CreateVisual(); | ||
box->AddGeometry(scene->CreateBox()); | ||
box->SetOrigin(0.0, 0.0, 0.0); | ||
box->SetLocalPosition(0.0, 0.0, 0.0); | ||
box->SetLocalRotation(0.0, 0.0, 0.0); | ||
box->SetLocalScale(1.0, 1.0, 1.0); | ||
root->AddChild(box); | ||
|
||
// API without RayQueryResult and default max distance | ||
result = ScreenToScene(centerClick, camera, rayQuery, rayResult); | ||
|
||
EXPECT_NEAR(0.5, result.Z(), 1e-10); | ||
EXPECT_NEAR(0.0, result.X(), 2e-6); | ||
EXPECT_NEAR(0.0, result.Y(), 2e-6); | ||
EXPECT_TRUE(rayResult); | ||
EXPECT_NEAR(14.5 - camera->NearClipPlane(), rayResult.distance, 4e-6); | ||
EXPECT_EQ(box->Id(), rayResult.objectId); | ||
|
||
result = ScreenToScene(centerClick, camera, rayQuery, rayResult, 20.0); | ||
|
||
EXPECT_NEAR(0.5, result.Z(), 1e-10); | ||
EXPECT_NEAR(0.0, result.X(), 2e-6); | ||
EXPECT_NEAR(0.0, result.Y(), 2e-6); | ||
EXPECT_TRUE(rayResult); | ||
EXPECT_NEAR(14.5 - camera->NearClipPlane(), rayResult.distance, 4e-6); | ||
EXPECT_EQ(box->Id(), rayResult.objectId); | ||
|
||
// Move camera closer to box | ||
camera->SetLocalPosition(0.0, 0.0, 7.0); | ||
camera->SetLocalRotation(0.0, IGN_PI / 2, 0.0); | ||
|
||
result = ScreenToScene(centerClick, camera, rayQuery, rayResult); | ||
|
||
EXPECT_NEAR(0.5, result.Z(), 1e-10); | ||
EXPECT_NEAR(0.0, result.X(), 2e-6); | ||
EXPECT_NEAR(0.0, result.Y(), 2e-6); | ||
EXPECT_TRUE(rayResult); | ||
EXPECT_NEAR(6.5 - camera->NearClipPlane(), rayResult.distance, 4e-6); | ||
EXPECT_EQ(box->Id(), rayResult.objectId); | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
TEST_P(UtilTest, ClickToScene) | ||
{ | ||
ClickToScene(GetParam()); | ||
} | ||
|
||
INSTANTIATE_TEST_CASE_P(ClickToScene, UtilTest, | ||
RENDER_ENGINE_VALUES, | ||
ignition::rendering::PrintToStringParam()); | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
::testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
global functions start with lower case. Can we change this to
screenToScene
? Same goes for the other functions added in this class.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ahh I'm a few mins late
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#368