Google
 
Back to index
CGAL 是基于 OpenGL 的一个计算机图形库,是大多数计算机图形学科研、学习等工作的必备利器。下面就简单介绍一下在 Windows 下安装与使用 OpenGL 的方法。

Windows 下用 VC++ 2005 来安装 CGAL 库

1. 准备工作

开始安装前,请仔细读一遍 CGAL 3.5 的官方安装手册

CGAL 依赖于 CMake、Boost 和 Qt 库,所以,我们需要先安装这三个工具。

2. 安装 CMake

安装 CMake 很简单,直接下载 exe 文件进行安装即可。

3. 安装 Boost 库

在 Windows 下最简单的安装 Boost 库的方法,就是用 bootstrap 来实现。本方法适用于 boost 1.40 版本。

下载 Boost 源代码并解压,进入 Boost 源代码目录,用命令:

bootstrap
.\bjam

进行安装。

4. 安装 Qt,并用 Visual Studio 2005 编译

CGAL 的例子程序都是用 Qt 来构建界面的,如果我们需要使用 CGAL 的例子程序,则我们需要在 Visual Studio 2005 里编译出 Qt 库文件。有关如何通过 Visual Studio 2005 来编译与使用 Qt,请参考这篇文章

5. 安装 CGAL

建议把 CGAL 安装到目录 C:\CGAL\CGAL-3.4 ,当然,你也可以使用其它路径,不过这个路径最好是不包含空格的一个路径。

先从 CGAL 的下载页面下载针对 Visual C++ 的 CGAL 安装版本,运行 exe 文件进行安装,CGAL 会把源代码包解压出来,并且会从网上下载几个库文件,这几个库文件为:

6. 在 Visual Studio 2005 里编译 CGAL

跑完安装过程后,我们就可以用 CMake 来为 CGAL 生成 Visual Studio 2005 的工程文件,这个过程最好用 CMake 的图像化界面安装方法。进入 CGAL 的安装目录后用 cmake-gui . 命令(注意:后面的那个 . 号不能少):

cd  CGAL-3.5
cmake-gui . # Notice the dot to indicate the current directory.

在弹出窗口后,我们按 Configure 按钮进行配置,大致示意图如图 1 所示:

图3 CMake Configure
图3 CMake Configure

如果按 Configure 后,下面的窗口提示类似如下的错误(片断):

CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
Boost_INCLUDE_DIR (ADVANCED)

这是因为 CMake 维护着自己的一些变量,这个和 Windows 系统的环境变量是有区别的。

我们需要把 BOOST_ROOT 或 Boost_INCLUDE_DIR 这个变量加到 CMake 的变量里去,值为 Boost 的安装目录,我们这里是 D:\boost_1_40_0。点 Add Entry 即可添加该值,如图 2 所示:

图2 Add Boost Root
图2 Add Boost Root

把 BOOST_ROOT 这个变量添加到 CMake 里后,再 Configure,如果是第一次点击 Configure ,会出现很多行红色的提示(如图 1 中的红色行)。只要下面的窗口没有提示红色的错误,就不用理会,再按一次 Configure 后,Generate 按钮就生效了。这个时候,点击 Generate 就可以生成 Visual Studio 2005 的工程文件了。

打开这些 Visual Studio 2005 的工程文件,就可以对 CGAL 进行编译了。

编译完成后,就会在 CGAL 的安装目录下,生成一个 lib 文件夹存放相关的库文件。你写程序的时候,就要记得把这个库文件添加到工程目录里。

7. 安装

如果你在这个工程里,点击运行了 INSTALL 这个工程,则会在你指定的目录里安装 CGAL 及相关的包。

默认情况下是 C:\Program Files\CGAL 。

当然,这一步并不一定是需要的。你可以直接使用你编译后的目录里的东西。

8. 写一个运行程序

现在可以用一段例子代码来运行了,新建一个 Hello World 的工程,然后把下面的这段代码拷进去,如果你的环境配置得当的话,就可以运行了:

#include <CGAL/Homogeneous.h>
#include <CGAL/Width_default_traits_3.h>
#include <CGAL/Width_3.h>
#include <iostream>
#include <vector>

#if defined(CGAL_USE_GMP)
#include <CGAL/Gmpz.h>
typedef CGAL::Gmpz                           RT;
#elif defined (CGAL_USE_LEDA)
#include <CGAL/leda_integer.h>
typedef leda_integer                          RT;
#else
#include <CGAL/MP_Float.h>
typedef CGAL::MP_Float                        RT;
#endif

typedef CGAL::Homogeneous<RT>                 Kernel;
typedef Kernel::Point_3                       Point_3;
typedef Kernel::Plane_3                       Plane_3;
typedef CGAL::Width_default_traits_3<Kernel>  Width_traits;
typedef CGAL::Width_3<Width_traits>           Width;

int main() {
    // Create a simplex using homogeneous integer coordinates
    std::vector<Point_3> points;
    points.push_back( Point_3(2,0,0,1));
    points.push_back( Point_3(0,1,0,1));
    points.push_back( Point_3(0,0,1,1));
    points.push_back( Point_3(0,0,0,1));

    // Compute width of simplex
    Width simplex( points.begin(), points.end());

    // Output of squared width, width-planes, and optimal direction
    RT wnum, wdenom;
    simplex.get_squared_width( wnum, wdenom);
    std::cout << "Squared Width: " << wnum << "/" << wdenom << std::endl;

    std::cout << "Direction: " << simplex.get_build_direction() << std::endl;

    Plane_3  e1, e2;
    std::cout << "Planes: E1: " << e1 << ".  E2: " << e2 <<std::endl;

    std::cout << "Number of optimal solutions: "
              << simplex.get_number_of_optimal_solutions() << std::endl;
    return(0);
}