PlaneSourceDemo & conanfile.txt

40 篇文章 7 订阅

step 1

conanfile.txt

[requires]
VTK/8.2.0@imaging/stable

[generators]
cmake

step 2

mkdir build
cd build
conan install ../conanfile.txt -s arch=x86 -r cloud --update

step 3

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

set(TARGET_NAME "TestProject")
project(${TARGET_NAME})

cmake_policy(SET CMP0043 NEW)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_POSITION_INDEPENDENT_CODE ON)
add_definitions("-std=c++11")

#SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall -g2 -ggdb")  # enable debug info for release version
if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE "Release")
  message(STATUS "Build type not specified: Use Release by default")
endif(NOT CMAKE_BUILD_TYPE)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

FIND_PACKAGE( OpenMP REQUIRED)
IF(OPENMP_FOUND)
    message("OPENMP FOUND")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
ENDIF()

message("CMAKE_BINARY_DIR = ${CMAKE_BINARY_DIR}")
message("CMAKE_BUILD_TYPE = ${CMAKE_BUILD_TYPE}")

add_subdirectory(test)

step 4

test/CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
set(TARGET_NAME "Demo")
project(${TARGET_NAME})

cmake_policy(SET CMP0043 NEW)

#SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall -g2 -ggdb")  # enable debug info for release version
if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE "Release")
  message(STATUS "Build type not specified: Use Release by default")
endif(NOT CMAKE_BUILD_TYPE)


FILE(GLOB SOURCE_FILES "*.cxx")

add_executable(${TARGET_NAME} ${SOURCE_FILES})

if(MSVC)
 target_compile_options(${TARGET_NAME} PRIVATE "/MP")
endif()

if (UNIX)
    target_link_libraries(${TARGET_NAME} -Wl,--start-group pthread)
endif (UNIX)

if (WIN32)
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /LARGEADDRESSAWARE")
endif (WIN32)

foreach(_LIB ${CONAN_LIBS_RELEASE})
    target_link_libraries(${TARGET_NAME} optimized ${_LIB})
endforeach()
foreach(_LIB ${CONAN_LIBS_DEBUG})
    target_link_libraries(${TARGET_NAME} debug ${_LIB})
endforeach()

target_link_libraries(${TARGET_NAME})

if (WIN32)
    target_link_libraries(${TARGET_NAME} rpcrt4 opengl32 wsock32 ws2_32 dbghelp)
endif (WIN32)


step 5

test/PlaneSourceDemo.cxx

#include <vtkSmartPointer.h>
#include <vtkPlaneSource.h>
#include <vtkSphereSource.h>
#include <vtkArrowSource.h>

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkTransform.h>
#include <vtkTransformPolyDataFilter.h>
#include <vtkNamedColors.h>
#include <vtkColor.h>
#include <vtkLegendBoxActor.h>

#include <vtkMinimalStandardRandomSequence.h>

#include <array>

#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkInteractionStyle)
VTK_MODULE_INIT(vtkRenderingFreeType)
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2)


namespace
{
vtkSmartPointer<vtkPolyData> CreateArrow(
  double &length,
  std::array<double,3> &startPoint,
  std::array<double,3> &endPoint);
}

int main(int, char *argv[])
{
  vtkSmartPointer<vtkNamedColors> colors =
    vtkSmartPointer<vtkNamedColors>::New();
  vtkColor3d backgroundColor = colors->GetColor3d("SlateGray");
  vtkColor3d legendBackgroundColor = colors->GetColor3d("Black");
  vtkColor3d originColor = colors->GetColor3d("Tomato");
  vtkColor3d centerColor = colors->GetColor3d("Banana");
  vtkColor3d point1Color = colors->GetColor3d("Peru");
  vtkColor3d point2Color = colors->GetColor3d("Bisque");
  vtkColor3d xAxisColor = colors->GetColor3d("lime");
  vtkColor3d yAxisColor = colors->GetColor3d("orange");
  vtkColor3d normalColor = colors->GetColor3d("Raspberry");

// Create actors
  vtkSmartPointer<vtkPlaneSource> planeSource =
    vtkSmartPointer<vtkPlaneSource>::New();
  planeSource->SetOrigin(0.0, 0.0, 0.0);
  planeSource->SetPoint1(1,0, 0);
  planeSource->SetPoint2(0,1.0, 0);
  planeSource->Update();

  std::array<double, 6> bounds;
  planeSource->GetOutput()->GetBounds(bounds.data());
  double length = std::max(bounds[1] - bounds[0], bounds[3] - bounds[2]);

  vtkSmartPointer<vtkPolyDataMapper> planeMapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  planeMapper->SetInputConnection(planeSource->GetOutputPort());
  vtkSmartPointer<vtkActor> planeActor =
    vtkSmartPointer<vtkActor>::New();
  planeActor->SetMapper(planeMapper);

  vtkSmartPointer<vtkSphereSource> sphereSource =
    vtkSmartPointer<vtkSphereSource>::New();
  sphereSource->SetRadius(length * .04);
  vtkSmartPointer<vtkPolyDataMapper> originMapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  originMapper->SetInputConnection(sphereSource->GetOutputPort());
  vtkSmartPointer<vtkActor> originActor =
    vtkSmartPointer<vtkActor>::New();
  originActor->SetPosition(planeSource->GetOrigin());
  originActor->SetMapper(originMapper);
  originActor->GetProperty()->SetDiffuseColor(originColor.GetData());

  vtkSmartPointer<vtkPolyDataMapper> centerMapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  centerMapper->SetInputConnection(sphereSource->GetOutputPort());
  vtkSmartPointer<vtkActor> centerActor =
    vtkSmartPointer<vtkActor>::New();
  centerActor->SetPosition(planeSource->GetCenter());
  centerActor->SetMapper(centerMapper);
  centerActor->GetProperty()->SetDiffuseColor(centerColor.GetData());

  vtkSmartPointer<vtkPolyDataMapper> point1Mapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  point1Mapper->SetInputConnection(sphereSource->GetOutputPort());
  vtkSmartPointer<vtkActor> point1Actor =
    vtkSmartPointer<vtkActor>::New();
  point1Actor->SetPosition(planeSource->GetPoint1());
  point1Actor->SetMapper(point1Mapper);
  point1Actor->GetProperty()->SetDiffuseColor(point1Color.GetData());

vtkSmartPointer<vtkPolyDataMapper> point2Mapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  point2Mapper->SetInputConnection(sphereSource->GetOutputPort());
  vtkSmartPointer<vtkActor> point2Actor =
    vtkSmartPointer<vtkActor>::New();
  point2Actor->SetPosition(planeSource->GetPoint2());
  point2Actor->SetMapper(point2Mapper);
  point2Actor->GetProperty()->SetDiffuseColor(point2Color.GetData());

  std::array<double, 3> center;
  std::array<double, 3> origin;
  std::array<double, 3> normal;
  std::array<double, 3> point1;
  std::array<double, 3> point2;
  for (auto i = 0; i < 3; ++i)
  {
    point1[i] = planeSource->GetPoint1()[i];
    point2[i] = planeSource->GetPoint2()[i];
    origin[i] = planeSource->GetOrigin()[i];
    center[i] = planeSource->GetCenter()[i];
    normal[i] = planeSource->GetNormal()[i] * length;
  }
  vtkSmartPointer<vtkPolyData> xAxisPolyData =
    CreateArrow(length, origin, point1);
  vtkSmartPointer<vtkPolyDataMapper> xAxisMapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  xAxisMapper->SetInputData(xAxisPolyData);
  vtkSmartPointer<vtkActor> xAxisActor =
    vtkSmartPointer<vtkActor>::New();
  xAxisActor->SetMapper(xAxisMapper);
  xAxisActor->GetProperty()->SetDiffuseColor(xAxisColor.GetData());

  vtkSmartPointer<vtkPolyData> yAxisPolyData =
    CreateArrow(length, origin, point2);
  vtkSmartPointer<vtkPolyDataMapper> yAxisMapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  yAxisMapper->SetInputData(yAxisPolyData);
  vtkSmartPointer<vtkActor> yAxisActor =
    vtkSmartPointer<vtkActor>::New();
  yAxisActor->SetMapper(yAxisMapper);
  yAxisActor->GetProperty()->SetDiffuseColor(yAxisColor.GetData());

  vtkSmartPointer<vtkPolyData> normalPolyData =
    CreateArrow(length, origin, normal);
  vtkSmartPointer<vtkPolyDataMapper> normalMapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  normalMapper->SetInputData(normalPolyData);
  vtkSmartPointer<vtkActor> normalActor =
    vtkSmartPointer<vtkActor>::New();
  normalActor->SetMapper(normalMapper);
  normalActor->GetProperty()->SetDiffuseColor(normalColor.GetData());

// Create the RenderWindow, Renderer
//
  vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();

  vtkSmartPointer<vtkLegendBoxActor> legend =
      vtkSmartPointer<vtkLegendBoxActor>::New();
  legend->SetNumberOfEntries(7);
  legend->UseBackgroundOn();
  legend->SetBackgroundColor(legendBackgroundColor.GetData());
  legend->GetPositionCoordinate()->SetValue(.7, .7);
  legend->GetPosition2Coordinate()->SetValue(.3, .3);
  int entry = 0;
  legend->SetEntry(entry++, sphereSource->GetOutput(), "center",
                   centerColor.GetData());
  legend->SetEntry(entry++, sphereSource->GetOutput(), "origin",
                   originColor.GetData());
  legend->SetEntry(entry++, sphereSource->GetOutput(), "point1",
                   point1Color.GetData());
  legend->SetEntry(entry++, sphereSource->GetOutput(), "point2",
                   point2Color.GetData());
  legend->SetEntry(entry++, xAxisPolyData, "xAxis",
                   xAxisColor.GetData());
  legend->SetEntry(entry++, xAxisPolyData, "yAxis",
                   yAxisColor.GetData());
  legend->SetEntry(entry++, xAxisPolyData, "normal",
                   normalColor.GetData());

  vtkSmartPointer<vtkRenderWindow> renderWindow =
    vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);
  renderWindow->SetSize(640, 480);

vtkSmartPointer<vtkRenderWindowInteractor> interactor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  interactor->SetRenderWindow(renderWindow);

  renderer->SetBackground(backgroundColor.GetData());
  renderer->AddActor(planeActor);
  renderer->AddActor(originActor);
  renderer->AddActor(centerActor);
  renderer->AddActor(point1Actor);
  renderer->AddActor(point2Actor);
  renderer->AddActor(xAxisActor);
  renderer->AddActor(yAxisActor);
  renderer->AddActor(normalActor);
  renderer->AddActor(legend);

  renderWindow->Render();
  renderer->GetActiveCamera()->SetPosition(1, 0, 0);
  renderer->GetActiveCamera()->SetFocalPoint(0, 1, 0);
  renderer->GetActiveCamera()->SetViewUp(0, 0, 1);
  renderer->GetActiveCamera()->Azimuth(30);
  renderer->GetActiveCamera()->Elevation(30);
  renderer->ResetCamera();
  renderWindow->Render();
  interactor->Start();

  return EXIT_SUCCESS;
}

namespace
{
vtkSmartPointer<vtkPolyData> CreateArrow(
  double &pdLength,
  std::array<double,3> &startPoint,
  std::array<double,3> &endPoint)
{
  vtkSmartPointer<vtkPolyData> polyData;

  //Create an arrow.
  vtkSmartPointer<vtkArrowSource> arrowSource =
    vtkSmartPointer<vtkArrowSource>::New();
  arrowSource->SetShaftRadius(pdLength * .01);
  arrowSource->SetShaftResolution(20);
  arrowSource->SetTipLength(pdLength * .1);
  arrowSource->SetTipRadius(pdLength * .05);
  arrowSource->SetTipResolution(20);

  // Compute a basis
  std::array<double, 3> normalizedX;
  std::array<double, 3> normalizedY;
  std::array<double, 3> normalizedZ;

  // The X axis is a vector from start to end
  vtkMath::Subtract(endPoint.data(), startPoint.data(), normalizedX.data());
  double length = vtkMath::Norm(normalizedX.data());
  vtkMath::Normalize(normalizedX.data());

  // The Z axis is an arbitrary vector cross X
  vtkSmartPointer<vtkMinimalStandardRandomSequence> rng =
    vtkSmartPointer<vtkMinimalStandardRandomSequence>::New();
  rng->SetSeed(8775070); // For testing.

  std::array<double, 3> arbitrary;
  for (auto i = 0; i < 3; ++i)
  {
    rng->Next();
    arbitrary[i] = rng->GetRangeValue(-10, 10);
  }
  vtkMath::Cross(normalizedX.data(), arbitrary.data(), normalizedZ.data());
  vtkMath::Normalize(normalizedZ.data());

  // The Y axis is Z cross X
  vtkMath::Cross(normalizedZ.data(), normalizedX.data(), normalizedY.data());
  vtkSmartPointer<vtkMatrix4x4> matrix =
    vtkSmartPointer<vtkMatrix4x4>::New();

  // Create the direction cosine matrix
  matrix->Identity();
  for (auto i = 0; i < 3; i++)
  {
    matrix->SetElement(i, 0, normalizedX[i]);
    matrix->SetElement(i, 1, normalizedY[i]);
    matrix->SetElement(i, 2, normalizedZ[i]);
  }
  std::cout << "matrix:\n" << matrix << "\n";

  // Apply the transforms
  vtkSmartPointer<vtkTransform> transform =
    vtkSmartPointer<vtkTransform>::New();
  transform->Translate(startPoint.data());
  transform->Concatenate(matrix);
  transform->Scale(length, length, length);

  // Transform the polydata
  vtkSmartPointer<vtkTransformPolyDataFilter> transformPD =
    vtkSmartPointer<vtkTransformPolyDataFilter>::New();
  transformPD->SetTransform(transform);
  transformPD->SetInputConnection(arrowSource->GetOutputPort());
  transformPD->Update();
  polyData = transformPD->GetOutput();
  return polyData;
}
}

step 6

cmake

step 7 

results

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值