首页 > > 详细

ENGN2912B程序讲解、辅导C++程序设计、c/c++编程语言调试辅导留学生 Statistics统计、回归、迭代|辅导Python程序

ENGN2912B 2020 | Lecture15 - 1
Brown University ENGN2912B Fall 2020
Scientific Computing in C++
Lecture 15 | The IfsViewer Application
In this lecture we will start to build an interactive application of moderate complexity for reading,
visualizing, operating on, and saving polygon meshes and point clouds. This is a much more complex
project than the ones we have worked on so far, which requires careful class design, partitioning the
project files into libraries, and also linking your program using external libraries. These libraries are part
of even more complex packages, which you will have to install in your machine, and configure for use in
conjunction with Qt.
Figure 1 The IfsViewer application shown running in OSX and Windows.
This application, as shown above running in OSX and Windows, will be extended in subsequent lectures
and homework assignments. What you will implement in this first assignment provides basic
functionality to load, save, and operate on polygon meshes and point clouds. It will be able to load a
polygon mesh or a point cloud from a file, visualize it, and save the polygon mesh or point cloud to a file.
In this first homework assignment you will implement the main data structures for representing a
polygon mesh or a point cloud in memory, a method to load a polygon mesh or point cloud from a file,
and a method to save the polygon mesh or point cloud to a file. You will implement the loading and
saving using the Factory framework, which we covered in a previous lecture. In subsequent homework
assignments you will implement additional loaders and savers to support other popular file formats used
to store polygon meshes and point clouds. For this assignment, all the user interface and OpenGL
graphics programming is provided. In subsequent assignments you will extend the application by
writing, in addition to the loaders and savers mentioned above, a number of geometry processing
operations. Since most of these algorithms do require additions to the user interface to set various
parameters, we will design a unified framework to add these user interface components, which you will
also implement in an incremental fashion.
ENGN2912B 2020 | Lecture15 - 2
Qt
The graphical user interface for IfsViewer is built using Qt. Your part of the code can be implemented in
its vast majority using the standard C++ classes, but you can also use Qt classes, if you prefer to do so.
We have discussed the installation process for Qt within the context of the QtCalc assignments. For this
assignment you should download the archive file Assignment8 archive from the course web site, unzip
it, and use CMake to build project files using the CMakeLists.txt file located in the src directory. In this
assignment we will not use QMake.
The goal is to complete the IfsViewer application implementation
I have decided to partition the project into three subprojects. The files in the directory
IfsViewer/src/viewer implement the user interface. In this assignment you don’t need to make any
changes to those files. The files in the directory IfsViewer/src/util implement some utility functions
which in this assignment you don’t have to change either. So far there is only one class in this directory.
The BBox class provides functionality to represent a bounding box containing a set of points in ddimensional
space. In this application we are only using it for d=3. One of the constructors builds a
bounding box from a set of points, stored as an instance of the vector class which stores all the
coordinates of the points as a linear array (x0,y0,z0),(x1,y1,z1),…,(xN,yN,ZN). Due to lack of time, the
BBox class is already implemented.
The VRML (Virtual Reality Modeling Language, pronounced vermal or by its initials) is an international
standard file format for representing 3-dimensional (3D) interactive vector graphics, designed
particularly with the World Wide Web in mind. It has been superseded by X3D, but still widely used.
https://en.wikipedia.org/wiki/VRML
As a reference, this is where you can read the VRML specification
http://www.web3d.org/documents/specifications/14772/V2.0/
You can download the VRML 2.0 - Cheat Sheat, by Jan Hardenbergh from the course web site. This is a
summary of all the VRML nodes.
The “Annotated VRML 97 Reference Manual” by Rikk Carey and Gavin Bell, provides additional
information about the VRML standard
http://accad.osu.edu/~pgerstma/class/vnv/resources/info/AnnotatedVrmlRef/about.htm
In Assignment 8 you will have to complete the implementation of two classes in the directories
IfsViewer/src/ifs. First of all you will have to complete the implementation of the Ifs class. This
class is intended to be a container for the information which can be represented in a VRML
IndexedFaceSet node. This is how the IndexedFaceSet node is defined in the VRML standard
IndexedFaceSet {
eventIn MFInt32 set_colorIndex
eventIn MFInt32 set_coordIndex
eventIn MFInt32 set_normalIndex
eventIn MFInt32 set_texCoordIndex
exposedField SFNode color NULL
exposedField SFNode coord NULL
exposedField SFNode normal NULL
exposedField SFNode texCoord NULL
field SFBool ccw TRUE
ENGN2912B 2020 | Lecture15 - 3
field MFInt32 colorIndex [] # [-1,)
field SFBool colorPerVertex TRUE
field SFBool convex TRUE
field MFInt32 coordIndex [] # [-1,)
field SFFloat creaseAngle 0 # [ 0,)
field MFInt32 normalIndex [] # [-1,)
field SFBool normalPerVertex TRUE
field SFBool solid TRUE
field MFInt32 texCoordIndex [] # [-1,)
}
We are going to ignore the eventIn fields, as well as whether fields are “exposed” or not, and to keep
things simpler we are not going to implement the color, coord, normal, and texCoord fields as
separate classes. Instead, the fields will be represented as follows in the Ifs class. Various methods to
access these private variables are defined in the Ifs.hpp header file. You have to implement them all.
Pay particular attention to understanding the convention adopted in the standard to represent property
bindings, and in particular you need to understand what is the meaning of properties bound
PER_VERTEX, PER_FACE, PER_FACE_INDEXED, and PER_CORNER, as well as what is the role of the four
index fields in all these cases.
class Ifs {
private:
bool _ccw;
bool _convex;
float _creaseAngle;
bool _solid;
bool _normalPerVertex;
bool _colorPerVertex;
vector _coord;
vector _coordIndex;
vector _normal;
vector _normalIndex;
vector _color;
vector _colorIndex;
vector _texCoord;
vector _texCoordIndex;
// …
}
The remaining files in this directory implement Factory frameworks to load instances of the Ifs class
from files, and to save instances of the Ifs class to files
IfsLoader.hpp
IfsSaver.hpp
IfsLoaderOpt.hpp
IfsLoaderOpt.cpp
IfsLoaderWrl.hpp
IfsLoaderWrl.cpp
IfsSaverWrl.hpp
IfsSaverWrl.cpp
IfsSaverFactory.hpp
IfsSaverFactory.cpp
ENGN2912B 2020 | Lecture15 - 4
You also have to implement the IfsLoaderWrl class using technique based on partitioning the input
stream into “tokens”. The other classes are already implemented, and you can use some as examples to
figure out how to implement the IfsLoaderWrl class. In subsequent assignments we will extend the
VRML parser, and will implement other parsers using the same methodology.
But we will first look at how to parse command line parameters, both for command line applications
such as those we have been writing since the beginning of the course. One important use for command
line parameters is to be able to invoke your programs to run without user interaction. Command line
parameters can be used to specify the names of input and output files, to specify values for Boolean
variables, also called “switches”, and to specify values for numerical variables.
For example, to debug the code that you need to implement for the IfsViewer application, and due to
lack of time, we are providing a command line application named ifstest which will take as arguments
a number of optional switches, the name of an input file, and the name of an output file. One of those
switches turns on and off the printing of console messages used to monitor the progress of the program.
More details about this application are provided in the ignment8 description file. The application will be
run from a console by typing the following command
> ifstest –debug inputFile.wrl outputFile.wrl
In this case the command line parameters is an array of four C-style (char*) strings composed of:
“ifstest”, “-debug”, “inputFile.wrl”, and “outputFile.wrl”. Note that the first parameter is the
name of the application itself.
Even though the application ifstest is already implemented, you should study it carefully, and if you have
xtra time you should try to implement your own version from scratch.
Parsing Command Line Parameters in Command Line Applications
A command line application executes the main() function. To get access to the command line
parameters you must declare main() as follows
int main(int argc char* argv) {
// process ...
}
The actual names of the arguments is not important, but this is the established convention: argc stands
for argument count, and argv for argument vector. Since argv[0] is always equal to the name of the
application, it is guaranteed that argc>=1.
For the example given above, we could try to process the command line parameters as follows
#include
int main(int argc, char** argv) {
bool debug = false;
string inFile = “”;
string outFile = “”;
if(string(argv[1])==”-debug”)
debug = true;
ENGN2912B 2020 | Lecture15 - 5
inFile = string(argv[2]);
outFile = string(argv[3]);
// load input file
// process ...
// save output file
return 0;
}
Note that since the command line parameters are passed as C-style strings, and we have decided to
implement this program using C++ strings, we convert the parameters from char* to string using the
string class constructor. We first initialize the variables debug, inFile, and outFile to default values,
and then we set them to the values specified by the command line parameters. If argv[1] is equal to
the string “-debug” then we set the debug variable to true. Then we set the string inFile to the value
specified by the command line parameter argv[2], and outFile to the value specified by the command
line parameter argv[3].
This program will work as long as it run as described above, with the three arguments. But it will fail for
example it is run without the “-debug” switch
> ifstest inputFile.wrl outputFile.wrl
In fact, this program most likely will crash while trying to set the value of the outFile variable, since the
fourth argument argv[3] would not be defined. To prevent crashes we need to implement the parsing
of command line parameters in a different way, where we analyze each command line parameter and
set variables depending on its values. At the same time, we need to detect errors in the command line
syntax, and exit the program reporting an error code in such cases. For example, consider the following
code fragment
#include
int main(int argc, char** argv) {
bool debug = false;
string inFile = “”;
string outFile = “”;
for(int i=1;i

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!