Tuesday, 15 March 2016

Understanding move semantics and rvalues in C and C++

http://eli.thegreenplace.net/2011/12/15/understanding-lvalues-and-rvalues-in-c-and-c/

A good example of the usage of move semantics and rvalue references. 

#include <algorithm>
#include <iostream>
using namespace std;
class Intvec
{
public:
explicit Intvec(size_t num = 0)
: m_size(num), m_data(new int[m_size])
{
log("constructor");
}
~Intvec()
{
log("destructor");
if (m_data) {
delete[] m_data;
m_data = 0;
}
}
Intvec(const Intvec& other)
: m_size(other.m_size), m_data(new int[m_size])
{
log("copy constructor");
for (size_t i = 0; i < m_size; ++i)
m_data[i] = other.m_data[i];
}
Intvec& operator=(const Intvec& other)
{
log("copy assignment operator");
Intvec tmp(other);
std::swap(m_size, tmp.m_size);
std::swap(m_data, tmp.m_data);
return *this;
}
//!!! move semantics - move assignment operator
// It takes the rvalue reference as its function parameter
Intvec& operator=(Intvec&& other)
{
log("move assignment operator");
std::swap(m_size, other.m_size);
std::swap(m_data, other.m_data);
return *this;
}
private:
void log(const char* msg)
{
cout << "[" << this << "] " << msg << "\n";
}
size_t m_size;
int* m_data;
};
int main()
{
Intvec v1(20);
Intvec v2;
cout << endl << endl;
cout << "assigning lvalue...\n";
v2 = v1;
cout << "ended assigning lvalue...\n";
cout << endl << endl;
//!!! here, instead of call the first copy assignment function, the move assignment function is called,
// which saves the overhead of creating another temporary object.
cout << "assigning rvalue...\n";
v2 = Intvec(33);
cout << "ended assigning rvalue...\n";
cout << endl << endl;
return 0;
}


Thursday, 10 March 2016

Job Interview Questions - C++

1. How to prevent a user from creating a class instance on the stack?

2. How to prevent a user from creating a class instance on the heap?

3. Smart pointers

4. C++ 11 features

5. Data structures:

map, unordered_map;

hash table, red-black tree;

Issues with hash table

6. Virtual table

- class or instance owned?

7. Usage of function pointers


Thursday, 3 March 2016

Wednesday, 2 March 2016

Computer Graphics Questions in Job Interview

1. When does Depth-Test take place in the openGL graphics pipeline? 

    (Before fragment shader, or after, or can be both?

2. What is the cause of Gimbol Lock?


3. Given two lines in a 3D space, how to calculate the distance between them?

Friday, 19 October 2012

Readings on Physically based Elastic Deformation


 (draft ...busy with confirmation report, coming back soon)

1  Textbooks for Continuum Mechanics

1. Continuum Mechanics

* A good material for the beginners to get to known the basis of continuum mechanics. I only read to the linear elasticity part.

2. Nonlinear Continuum Mechanics

Bonet, J. and R. D. Wood (2008). Nonlinear continuum mechanics for finite element analysis, 2nd Ed., Cambridge university press.
* Get a deeper understanding of the mathematics behind the continuum machanics, expecially the nonlinear elasticity. I read it through, but havenot got a deep understanding of the virtual work part,

3. Anisotropic Elasticity

Ting, T. C. T. (1996). Anisotropic elasticity: theory and applications, Oxford University Press, USA.
* Even deeper and more difficult mathematics. I just started reading it.

4.    Practical Time-stepping Schemes

Wood, W. (1990). Practical time-stepping schemes, Clarendon Press Oxford, UK.

2   ACM Siggraph Courses

Siggraph1997, Physically Based Modeling Principles and Practice;
Siggraph2001, Physically Based Modeling – similar to Siggraph1997;
Siggraph2008, Real Time Physics;
Siggraph2011, Destruction and Dynamics for Film and Game Production;
Siggraph2012, The classical FEM method and discretization methodology,
  Data-Driven Simulation Methods in Computer Graphics.

Hello world

Hello world! :)