Sidebar Menu

Projects

  • Dashboard
  • Research Project
  • Milestones
  • Repository
  • Tasks
  • Time Tracking
  • Designs
  • Forum
  • Users
  • Activities

Login

  • Login
  • Webmail
  • Admin
  • Downloads
  • Research

Twitter

Tweets by stumathews
Stuart Mathews
  • Home
  • Blog
  • Code
  • Running
  • Gaming
  • Research
  • About
    • Portfolio
    • Info

CMaking good progress

Details
Category: Blog
By Stuart Mathews
Stuart Mathews
30.Dec
30 December 2018
Last Updated: 30 December 2018
Hits: 2977
  • holiday
  • C++

Since A lot of doing a lot, I've made some significant progress with my C++ library. It compiles in Windows(Win10) and in Linux(Fedora 29, and WSL but you can't really call that Linux can you? Not according to Richard Stallman!)  and theoretically, it would work fine in Mac too(But I don't have a mac. maybe I'll send it to my dad for him to try).

I decided this time to rather than use Autotools as I did with my C library, I'd embrace CMake this time. 

CMake comes with Visual Studio support and supports creating shared libraries really easily. The major motivator behind this is actually twofold. I want to reuse some of the Math I've learnt while studying 3D programming in Direct X (which is only for Windows). The other part is I'd like to do a better job of stulibc as explained in Kim Kardashians butt and other stuff. 

Like my Autotools projects, I also have a suite of tests that will run that ensures that the library doesn't regress. The cool thing about this library is that it can be consumed by any C/C++ program as I've got two interfaces to the library. I've put all the C++ code into classes and I've used extern "C" {} code to export specific C functions which behind the scenes call the C++ classes. So this actually means that I can move a lot of my C code from stulibc and put it into  Stulibcpp

I was thinking that I'd probably spend a lot of time on this but I found resources on the net that showed me how it's done and I just improvised the rest. This one resource was about integrating leap motion(I remember the first time I saw one of these was when my colleagues at AppDNA brought one in) with Unity/UnReal.

One thing I did have to tweak was in Unix you can get the address of a function in a shared library with dlsym() like this as shown here:

# define GETFUNC(funchandle, lib, funcName) (*(void**)(&funchandle) = dlsym(lib, funcName))

// and call it like this:

int (*sub)(int, int);

GETFUNC(sub, "stucpp.so", "sub");

sub(10,5)

But this wasn't working for me, it was always complaining about how it cannot implicitly convert a void* to something else. This made sense as you can't do that in C++, though existing code online seemed to be using it. Anyway, to solve the problem I found this online which suggested doing this, which works great: 

int (*sub)(int, int);
reinterpret_cast<void*&>(sub) = dlsym(lib, "sub");
std::cout << sub(10,5) << std::endl;

I also re-used some of the ideas online to write C and C++ specific tests to ensure that the library would be able to be called correctly. For example, I've got a test that calls C-Style GetProcAddress() in windows and one test that merely links to the library and uses a compiled library class.

I'm relatively happy with that progress - now what needs to be done is to finish writing the bulk of the library.

As I explained in Kim Kardashians butt and other stuff, I'd like to do something similar to my C library but this time I'll be adding more Math functions, mostly around vector and matrix manipulations. I'll probably also throw in some Geometry and maybe even some collision detection stuff later one (Diff Calculus) which hopefully will allow me to link this stuff into any new C/C++ app.

Only one more day left of my holiday and this is about as far as I'd wanted to be! 

I think the initial plan will be to slowly port over some of the data structures in my C library or re-implement them with STL and then start putting together some easy file handling interfaces for dealing with saving config files etc as well as saving records to files etc.(in a generic fashion that is). 

I've limited my C++ to ISO 1998 so until I need things like lambdas and auto, I'll leave it as such. 

In other news, I met up with an old AppDNA/Citrix colleague and we compared notes around our existing projects.

One take away was that I should invest in learning google charts and Bootstrap material design for my front-end layout, particularly when it comes to visualizations.

I was actually pleasantly surprised how appealing some of his charts on his front end were. He'd done a great job of quickly developing a .Net MVC/Razor app that calculated financial metrics around companies' financial statements. It's a bit of fresh air really because when I try to develop a nice looking chart like this Running, Coffee, Chocolate and Pizza - it takes forever to be productive...a bit like C/C++!

I'd like to incorporate a few more visualizations in my Angular front end for finance tracker. Anyway, that's another story, which is further down on the list...

Now lets get cracking!

To infinity...and beyond!

 

 

A lot of doing a lot

Details
Category: Blog
By Stuart Mathews
Stuart Mathews
26.Dec
25 December 2018
Last Updated: 30 December 2018
Hits: 2278
  • C++
  • Math
  • DirectX

The last couple of days have seen me working mostly on learning the math involved DirectX 12. I've gotten to a point where I've got a good inkling as to how and more importantly why vertices have to be transformed from local coordinate space to world coordinate space. Less obvious however is the transformation to view and projection space but for now, I'm happy to know just how and why it needs to be done. I've formulated my thinking around Matrix algebra and other theory in DirectX math, coursework and some films

I took a bit of a break from my 3D graphics study and did some study of learning theories presented in my CPD coursework and also continued somewhat with my Software Engineering coursework. A good strategy I always find with this stuff is to read through it all first to get a general idea, then re-read it with more focus next time around. In this way, my speed reading capabilities have increased wonderfully. So I'm happy that I was able to be productive with my studies in this regard.

I went to the gym as per usual, albeit a little later than usual(seeing that have the time to go later) and I've rather enjoyed this. I also took out an interesting book Maths Ideas Really Need Know which is quite fun and has helped me appreciate a lot of stuff.

I've very recently(Christmas Eve) started reading my humongous Dietel C++ How to Program book which I'm really enjoying and it must be said that its a lot of fun to read because it's so exacting. The only thing is that its so big and I've had to change physical positions between each chapter. I've only got through the first 5 chapters and it's late now so I really must go to bed. I gave up an opportunity, though not begrudgingly, to watch Jurassic World for a little foray into arrays and pointers(what a life, eh?).

So it really has been a good holiday in general, I've done a lot but I've also rested a lot while doing a lot.

Just before I started reading the Dietel book, I started messing around with my tablet again(I recently replaced its SSD) and found that Ubuntu 14.04 was crashing a lot. I spend a lot of time(and bandwidth) trying to fix the problem, ultimately resorting to upgrading it which then ultimately failed and effectively 'bricked' the damned thing.

So I bit the bullet and downloaded Fedora 29 after an initial failed download of 1.7Gb! Fedora ultimately got installed and is currently running very well indeed. I've not given it the same time that Ubuntu had - so it still could reveal problems if indeed the problems are not software related - which I hope they are... But so far, so good - perhaps the latest software has remedied it. Silly bloody thing. I had my dad on call to give me Linux advice which is a privileged position to be for most children!

I suspect however that my tablet will not see much action, as it hasn't featured much in my time thus far.

An interesting development. which has luckily passed now was I strained my back a little during deadlifts on Monday, I felt it was a bit tight and strained on a few lifts and after my workout, I was a little tender. Doing this is kinda unusual for me, as I usually am quite careful and attentive to my body's messaging so that why I though that this was so interesting. Anyway, thankfully the rest I had took paid off.

 I've finally finished reading the bulk of the Dietel book having covered STL. I've Covered control structures, Functions, Arrays, Pointer and C-style strings, Classes and data abstraction, operator overloading. polymorphism, templates, Streams, Exceptions. Files, Strings, Preprocessor and the Standard Template Library. So I've got a good handle on the theory, now I just need to get stuck into making my C++ Library which used Autotools however I might consider CMake this time.

To infinity... and beyond!

DirectX math, coursework and some films

Details
Category: Blog
By Stuart Mathews
Stuart Mathews
18.Dec
17 December 2018
Last Updated: 28 December 2018
Hits: 3684
  • Math
  • DirectX

The last couple of days have been fairly interesting. I decided to learn about Vectors and the Math behind working with them. Things like what the dot product, unit vectors are and I did a bit of cursory trigonometry. Math has become a lot more fun for me since learning about games and 3d programming. Also I learnt how to embed math equasitions in by blog:

\[ det A = \sum_{j=1}^{n} A_{ij}(-1)^{i+j} det \bar{A}_{ij} \\ \tag{Determinant of A} \\ \]

The interesting thing about learning this stuff is not that I need it to work Direct X, which I do, but more so because it's nice to know what they mean and why it works. I think that's always been by a problem: sometimes I'm more interested in the concept sometimes than actually using it. But I think it does affect a lot of people too -  only they don't spend as much time on it as I do I think.

 

I remember when one of our Australian developers went to town converting all his C++ code to use templates... I think he loved the idea of templates more than perhaps the readability or productivity that the code might afford. All because templating in C++ was interesting. I sympathise. 

Anyway, I've also started reading more up on the Software Engineering coursework I have, it actually a lot more interesting than the CPD course, probably because I'm more familiar with it. I'm hoping the CPD becomes more interesting and fun... The thing about the CPD course is that it's all just a bit fluffy, you have to reflect on your past activities and apply theory to it and its all a bit 'mushy'. My last degree was more in-your-face:  here is a database, run this python query on it and send the bytes to that file etc. much more measured and hands-on, I guess that's why I like programming and maybe why I apprciate the math I'm learning at the moment.(a bit more than Continous Professional Development!)

I hit the gym yesterday and did quite a long and extended session focusing on my legs and having run in, I decided to run back and boy was that a test of endurance. My plans to be a little more varied in my workout haven't yet come into fruition yet. 

I watched the new Aquaman Film which I actually really enjoyed. I thought that perhaps this would be a watered down superhero film because it's not a well-known character but it was good. I quite like the co-star Amber Hurd, she's beautiful. Always helps. 

I watched my favourite film, Gladiator last night and I never get used to it - its a great film. 

Anyway back to my Direct X studies, whilst reading this new book about direct X 12... I've learned about the fundamental behind Vector arithmetic, basically observing the geometric results of adding, subtracting and multiplying vectors or forces.

I also noticed that the initialization and basic start-up code I learned about in DirectX11 has been replaced with a concept of queues and lists and commands that you send and prioritise graphics instructions to the GPU. This is new.

Some interesting facts are that negating a vector flips its direction, multiplying one, extends or rather scales its magnitude in the same direction and if you add two vectors (or forces) you can see the resulting displacement of an object or just the resultant vector by parallel extending the opposite vectors(I'll look for a good picture soon).

Translation in 3d Graphics terminology merely refers to taking a vector and putting it somewhere else - the direction and magnitude of the vector remain the same. Oh, and of course, a vector is represented geometrically as [x, y, z] and if you use the coordinate system, then you can see which direction this represents.

Some of the interesting things you can do on vectors is determining the vector magnitude using a special formula(which I can't show you right now because I can't get latex to work right on my blog). No wait I can:

\[ ||u|| = \sqrt{x^2 + y^2 + z^2} \\ \tag{eq.1.1 Magnitude of vector} \\ \]

The other is normalising the vector(which creates what is called a unit vector) which can be worked-out by applying a formula which basically just reduces the magnitude of the vector to 1 but the direction is still the same but obviously if you reduce the vector's magnitude, it's x, y, z coordinates will change, and that formula works out the newly adjusted values for them .

The other interesting thing you can do with two vectors is to determine if the angle between two vectors is acute, obtuse or perpendicular. This can be done by getting the scalar product (or the dot product) which is a sort of combination of two Vectors.

The other combination is the cross-product which allows you to determine a new vector which is orthogonal to the two vectors concerned - this is also called the left-hand rule, as the left hand rule also shows you(on your hand) where the orthogonal vector will be when given the direction of the first vector(which you aim your fingers at) and then curl your fingers towards to 2nd vector and then your thumb shows you where the perpendicular vector is. Kinda cool. 

The other thing you can do with unit vectors(magnitude is 1) is that you can easily determine the angle between the vectors (but not by using the dot product as that only tells you if the angle will be perpendicular, acute or obtuse) but by using cosine if both vectors are unit vectors. The equation for doing that is

\[ uv = ||u||||v|| \cos \theta \] and ||u|| and ||v|| just mean the magnitude of the vector and as these will both be 1, you can solve for the angle which will be the angle between the two vectors. 

My mind started to have a collapse when we started going over orthogonalising vectors which is something I'll have to look into at a later stage but basically its a formula to determine perpendiculars to vectors (like the left-hand rule) but with math. And the other brain-freezer was orthogonal projection, which I'm sure is all easy but I need an alternative perspective before my brain lets these ideas in!

These are the general equasions I learnt about when using vectors:

\[ ||u|| = \sqrt{x^2 + y^2 + z^2} \\ \tag{eq.1.1 Magnitude of vector} \\ \]

\[ \hat{u} = \frac{u}{||u||} = \frac{x}{||u||} + \frac{y}{||u||} + \frac{z}{||u||} \\ \tag{eq 1.2 Unit vector} \\ \]

\[ u \cdot v = u_{x}v_{x} + u_{y}v_{y} + u_{z} + v_{z} \\ \tag{eq 1.3 dot product} \\\]

\[ u \cdot v = cos \theta  \rightarrow ||u||||v|| = 1 (unit vectors) \\ \tag{eq 1.4 angle between vectors u and v} \\ \]

\[ w = u \times v = u_yv_z - u_zv_y, u_zv_x -u_xv_z, u_xv_y - y_yv_x \\ \tag{eg 1.5 cross product of two vectors} \\  \] 

I also learned about how to store Vectors in C++/DirectX using the SIMD registers that are part of SSE2 using the XMVECTOR Structure to store 4 32-bit floating point numbers which can represent a vector's parts ie [x, y, z] and the 4th float is not used. The other key idea was that use should only use this structure if you're storing these structures globally as this ensures some alignment requirements to be enforced. When this is not the case, Ie you want to store a vector in a class, you should use the XMFLOAT2,3,4 structure to store a Vector as it holds 2,3,4 floating point numbers which can represent your vector.

The cool thing about these structures is that they are 128-bit structures and one SIMD register is 128-bit and you can do one complete SIMD operation using 128bits stored in a SIMD register which is what DirectX does with these structures that you pass in. This is for efficiency purposes. Quite interesting.

A lot of the stuff I learnt was quite interesting because the proofs were solved, not just merely stating the equations but explaining the reasons behind the equations which is the right way to do it in my opinion.

Today I read up on Matrices, which are compact forms in which you can store Vectors or any 3 or 4 valued number(such as triangle vertices!). There are also some interesting things you can do on matrices. Basically, you can add/subtract and combine matrices too. There are generic equations which allow you to do this in a compact way but I was shown how these equations were derived from their geometric interpretations which were fun.

These were the ideas behind :

  • Multiplying matrices,
  • Vector-matrix multiplication - basically combining a vector with a matrix

\[ uA = xA_{1,*} + yA_{2,*} + zA_{3,*} \\ \tag{vector matrix multimplication} \\ \]

  • Transposing of a matrix - swapping a matrices columns with its rows
  • The identity matrix - a matrix that when multiplied by a matrix does not change the matrix
  • The determinant of  a matrix - which is a number which helps determine the inverse of a matrix

\[ det A = \sum_{j=1}^{n} A_{ij}(-1)^{i+j} det \bar{A}_{ij} \\ \tag{Determinant of A} \\ \]

  • Matrix Minors - a subset of a matrix which helps find the determinant of the matrix
  • Adjoint of a matrix - also helps to find the inverse of a matrix
  • The inverse of a matrix - which is used to help find another matrix when all others are provided in an equasion (so thats why its so useful.)

Equations:  (TODO)


\[ \left[
\begin{array}{ c c }
1 & 2 \\
3 & 4
\end{array} \right]
\]

Just like with Vectors most of the concepts mentioned above can be automatically calculated using DirectX helper functions (but where is the fun in doing that!)  but I'm thankful I know what that math behind it all is. The must say the Author Frank D Luna did a great job of explaining it. 

With Matrices, you can use a XMMATRIX instead of a  XMVECTOR and again you use this only for global variables of this type. To represent matrices in classes you would represent them as XMFLOAT4x4 which allows the storage of 4x4 Floats, which is the Matrix format most used in Direct X 12

 No I'm not sure I'll remember it all, but its probably going to start making sense when we start trying to translate, rotate, scale models that themselves are represented as vertices which are 3-valued structures and thus can be stored singularly as a Vector or an array of them, or indeed an array of Matrices. 

Tomorrow I'm going to read up about transformations(translate, rotate, scale) of these structures but first I'll need to figure out how orthogonal projection works - so let me get that out of the way first.

 

 

More Articles …

  1. Kim Kardashians butt and other stuff
  2. The value of life
  3. A dangerous miracle
  4. Software change, its needs and the rise of the MD
  5. Perhaps Coincidental, Perhaps Not
  6. Running, Coffee, Chocolate and Pizza
  7. Army Curious
  8. A commentary on Leadership
  9. IExtensibleDataObject and ExtensionDataObject
  10. Disposable and extra
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

Page 32 of 182