Tuesday, August 6, 2013

Signals3 Source Code and Performance Testing Update

Introduction

Since my last post on a fast thread-safe Signals/Slots implementation I've had to do a major re-write. The problem was that I had merged "slots" with nodes. This made implementing "self-disconnecting" slots near impossible (or at least, very inefficient). I also had structured my code quite poorly so managing includes was a nightmare.

Tuesday, July 30, 2013

Signals/Slots Improvements: Better and Faster

Introduction

Last time I presented basic work on my implementation of Signals/Slots which was thread safe, full-featured (well, is capable of being full-featured), and performed quite well. I had a decent implementation, but there were problems with passing iterators. I have figured out a better solution by making a small change. This improves performance, as well as easily allows the use of begin/end iterators rather than a single iterator which tracked all state.

Tuesday, July 23, 2013

Thread-Safe Signals/Slots using C++11

Introduction

For any C++ developer who's used Qt, we've grown to love the Signals/Slots idiom it presents for creating clean Observer code. However, it relied on the Qt Moc pre-compiler tool, which meant any project that wanted to use this feature had to use follow along with the Qt idiom, which really made Qt applications look potentially foreign despite being written in C++. In addition to this the implementation wasn't type-safe (a.k.a. no compile-time type checking), and doesn't generalize to any callable target (you have to extend QtObject and declare a slot using Qt's syntax, can only return void).

Since then, there have been multiple implementations of Signals/Slots. Some notable ones are listed below:

  • Boost Signals. Not thread safe, performance wasn't great, now deprecated in favor of Boost Signals2. Licensed under the Boost Liscense.
  • Boost Signals2. Thread safe upgrade of Boost Signals. Others have complained about its performance, but my tests seem to show it's at least decent.. Licensed under the Boost Liscense.
  • Libsigc++. Supposedly decently quick, but not thread safe. I think this is also a near-fully featured implementation like Boost Signals/Signals2. Also licensed under LGPL, making use somewhat restricted.
  • libtscb. A thread safe fairly quick quick implementation. However, it skimps on features (I think it offers similar features to Qt's implementation). Also licensed under LGPL.
  • C++11-based Implementation. This is actually another blog which sought to implement Signals/Slots using new features brought by C++11, namely variadic templates, std::function, and possibly more. This is CC0 licensed (public domain). Probably one of the fastest implementations I have seen, but is not thread-safe.

I was wondering if I could implement a more feature-full implementation like Boost Signals2 bet with better performance like libtscb. I'll be using some C++11 features like the last implementation, notably atomics, std::function, and variadic templates. I'm also using Boost shared_ptr/weak_ptr, Boost mutex, and Boost shared_ptr atomics. These libraries are being used because currently the compiler I'm using doesn't have the standard variants implemented (Mingw w64 gcc-4.8.1).

For the most part, I was able to implement (or actually, can see a straight-forward implementation) nearly all of the features provided by Boost Signals2. There are some semantic changes (notably the interface for combiners is different), and I was unable to capture the O(log(n)) performance of inserting group-ordered slots. My implementation likely will have O(n) group insertion.

Tuesday, June 25, 2013

Wheel Factorization with Variable Increments

Just a quick update to my previous wheel factorization code. I found a better way to implement the wheel factorization code which doesn't use bit masks to sieve out values. This version has a decent performance boost, and I think is easier to implement.

Tuesday, June 18, 2013

Rudimentary Wheel Factorization using Bit Masks

Time to have more fun with prime numbers! I've made a few updates to my sieving code making it faster and more robust. I've also worked through a basic wheel-factorization implementation, which had reasonable performance benefits.

Tuesday, June 11, 2013

SVG Navigable Viewport Part 4: The Quad Viewport and Fixing some Assumptions (again)

In my last post I had implemented viewport rotations, and I had assumed that I had fixed the problem with nested SVG elements. Unfortunately, as I started working on a "quad" viewport demo, I realized there were some problems with my assumptions about how SVG establishes new viewports. In short, I had to devise a new way to get the transformation matrix of an individual element.

Tuesday, June 4, 2013

SVG Navigable Viewport Part 3: Adding Rotations and Extending Functionality

This is a sample of how to rotate a SVG viewport using matrix transformations. There are two methods I explored. They basically are implemented near-identically, but operate somewhat differently. I'm also going to explore some slight changes to my previous implementation which made a few assumptions about the SVG DOM state.

Tuesday, May 28, 2013

SVG Navigable Viewport Part 2: Some Cross-Browser Support

So last time I presented a basic SVG viewport navigation strategy. I tested it in Chrome and it worked a treat. However, when I decided to test in the other browsers at my disposal (FF20, IE10), the nasty head of cross-platform incompatibility popped up in the strangest ways possible. I managed to add custom handlers for Firefox, but I didn't have time to look at IE10 or other browsers.

This time I'm going to try to figure out what gotcha's I have to solve to get IE10 working, seeing that it is currently the second most used browser. I would test Safari, but unfortunately the latest version is simply unavailable for me to test. I might try testing on Opera as well if I have time.

Tuesday, May 21, 2013

SVG Navigable Viewport Part 1: Theory and Frustrations

This is a continuation of my previous tests to try and develop a navigatable 2D scene using HTML5 and Scalable Vector Graphics. The features I want to discuss are:

  • Panning the scene
  • Scaling/Zooming in and out
  • Rotating the scene

Tuesday, May 14, 2013

Some Tests with HTML5 and Scalable Vector Graphics

These are a few tests with HTML and SVG vector graphics. There are a variety of good resources available online on how to use SVG, particularly the Mozilla Development Network (MDN). This is still a relatively new area of web development, and as such not every browser supports the SVG standard, and many don't support the full standard. I'm going to be focusing primarily on the "nitty-gritty" details so I won't be using GUI tools such as InkScape to create SVG (a.k.a. I'm going to be writing raw SVG code in a text editor, or use Javascript to generate/modify SVG elements).

Some good SVG resources:

Tuesday, May 7, 2013

Windows, NginX, MySQL, and PHP

Introduction

A while back I setup a LEMP server (Linux, NginX, MySQL, and PHP) using Virtual Box. This is fine for testing out how a live server might behave, or even for using as a virtual server if you don't have the dedicated hardware to host a server. However, for a server in active development it's a huge inconvenience as everytime I want to test out a change, no matter how small, I was forced to push the changes to the virtual server. To get around this, I'm going to setup a development server which can run in the Windows environment I normally use. I'm also going to setup the XDebug plugin. This will allow me to debug php code without using the "traditional" console/echo solution. I'm unsure if XDebug would work with my virtual server setup, but I do know it works with this development setup.

Tuesday, April 30, 2013

Online Transmission Line Simulation

This is a finite element simulation of the Telegrapher's Equations. I outlined the details of the simulation in my last post and had a SciPy implementation, and this time I've created a javascript port with user adjustable params. The simulation is only able to handle a point-to-point transmission line with a pulse step source. The termination resistor R1 is located at the source. There are 3 pre-defined scenarios which demonstrate issues with various termination resistances. I would recommend you read my other post for details on how the simulation works.

Tuesday, April 23, 2013

Transmission Line Simulation

Introduction

When dealing with fast changing signals or long transmission distances the signal propogation time becomes very important. In these cases we must use a transmission line model for signal wires. The basic loss-less model is a 4-terminal model with a distributed capacitance and inductance along the line. This model can be described with the Telegrapher's Equation. Basically this describes the propogation of signals using a 1D wave equation, who's general solution describes the superposition of two waves traveling in opposite directions. To visualize this effect I thought I would try to solve the Telegrapher's Equation using a lumped/finite element method. The basic idea is to divide the transmission line into a network of series inductors and parallel capacitors. The voltages and currents through the transmission line can be solved for using a transient circuit simulator.

I know that there exists a simplified SPICE model which can be used to easily and quickly solved for the currents/voltages at the ends of the transmission line. However, this model does not accurately model what happens inside the transmission line. There are also methods for solving the type of Partial Differential Equation described by the Telegrapher's Equation numerically. I chose the finite element method mainly because I wanted to learn more about how SPICE solvers work internally. This circuit should allow me to do that with a semi-complicated circuit. I also wanted to visualize the signal propogation of a pulse which can't be done using the SPICE transmission line model.

Tuesday, April 16, 2013

AVR Microcontrollers Part 2

Introduction

This is part 2 of my playing around with AVR microcontrollers. Last time, I had a basic setup which could program an AVR using the Arduino ISP. I used it to drive a simple 7-segment, multiplexed 4-digit LED display. This is a follow up where I try out some of the other features the ATTiny24A has to offer. I also decided to invest some money in an AVR Dragon programmer/debugger so I'll go over some basic playing and setup with this device. I'll also discuss some of the problems and pitfalls I've encountered along the way.

Tuesday, April 9, 2013

Segmented Sieve of Eratosthenes

From my last playing around with primes post, I've finally managed to segment the sieving algorithm. This does a few different things:

  • Reduces memory usage. Now the usage is proportional to the number of primes found plus some small fixed buffer.
  • Increases locality when sieving. This should help with performance.
  • Hopefully makes parallelization easier/possible.
  • Finding the first x primes becomes a relatively straightforward and quick task.

Tuesday, April 2, 2013

Optimizing the Tracking Pre-Regulator Design

In my last post I investigated a bit into a novel tracking pre-regulator design by John Barnes (I originally heard of this design in Dave Jones' EEVBlog #329 video). I had modeled mathematically the system rather than using SPICE simulation software as Dave did to play with the design and came up with a fairly nice model for figuring out what parameters to use. This is just a slight update as I investigated the design a bit further. In this post I'm going to present a novel way to make the transistor gain error nearly negligible.

Tuesday, March 26, 2013

Tracking Pre-Regulator Design Analysis

Introduction

I was watching a rather interesting EEVBlog video where Dave Jones showed a tracking pre-regulator design someone named "John Barnes" sent him (hopefully I got that right). Unlike his first video where he took Linear Technologies example circuit which uses the threshold voltage of a MOSFET to set the pre-regulator voltage, the new design uses a PNP transistor and resistors to calibrate the pre-regulator voltage. This design has several advantages over the MOSFET design. The main big one is that the threshold voltage can be set indepedendant of the specific component parts used, where-as the MOSFET design was highly dependant on the exact part used. Additionally, the ability to find the specific MOSFET with the correct threshold was unlikely. Dave did say you could compensate for this by adding diodes, but really this is a poor design in my opinion and he seemed to agree. Dave did some spice simulations with some actual parts and was able to parametrically evaluate what resistor values to use for a given target pre-regulator voltage. This is great if you have the available parts in your simulation software, but I wondered if I couldn't arrive at the solution using some math so I could design with any components I wanted. I also was curious as to why the thing worked to begin with, which the simulation software doesn't give too much indication towards.

Tuesday, March 19, 2013

More Fun with Sieving Primes

I was playing around with my prime sieving implementation and I managed to improve the performance by making some simple changes. The code looks quite a bit different, but functionally it does basically the same thing. The one big difference is that I switched the code to use arrays instead of ArrayLists. Through some testing I realized that there was a fairly significant performance difference between arrays and ArrayLists. The actual problem has to deal with Java generics and boxing of primitives. I may go more into the details at a later date. It's a rather specialized optimization and for the vast majority of programs shouldn't be necessary. Other than that, the code is generally more readable.

Tuesday, March 12, 2013

A Safer Way to test the Validity/Safety of a Link

One thing that's always bothered me is figuring out if a link is legitimate or potentially a security risk. This is especially problematic with shortened URL's. I really dislike these and don't follow shortened URLs unless I absolutely know it's a trusted link. Many times just knowing the poster isn't enough to convince me to click a shortened URL as it's way to easy for someone to have their account compromised.

So how do you analyze the contents of a target link in a safer environment other than test-clicking willy-nilly in a browser?

Tuesday, March 5, 2013

That One Bug That Just Won't Go Away...

So I was recently trying to get a piece of C++ code to compile and I just could not figure out what was going on. Here's a sample roughly of what the code looked like. It's not quite the same code I was debugging, but it does spit out similar compiler outputs.

namespace space
{

template<typename type> class ClassA
{
protected:
 int bounds;
public:
 virtual ~ClassA(void)
 {
 }

 virtual int doIt(void) = 0;
 virtual int doIt2(void) = 0;
};

template<typename type> class ClassB : public ClassA<type>
{
protected:
 ClassA<type> *parent;
public:
 virtual ~ClassB(void)
 {
 }

 virtual int doIt(void)
 {
  return bounds;
 }
 
 int doIt2(void)
 {
  return parent->bounds;
 }
};

}

For the longest time I could not figure out why doIt2 wasn't calling properly. The actual code I was debugging was a bit more complicated, but I should have been able to quickly identify the problem and fix it.

The compiler message: 'ClassA::bounds' : cannot access protected member declared in class 'ClassA'

The message would highlight the access line in doIt2 where I try to retrieve the bounds value from parent. My first thought was that the problem might have to deal with C++ template types not being right since I'm not particularly well-versed with C++ templates. After double checking, then triple and quadruple checking the templates I decided that they were correct and that there must be some other problem. Needless to say, I chased red herring after red herring trying to figure out what was wrong.

So what was the actual problem? I forgot to declare ClassB as a friend to ClassA. This is one of those details that gets lost between Java programming and C++ programming. In Java, even though the field bounds is protected I can still access it from anywhere in the same package. The closest equivalent in C++ are namespaces, though these really aren't that close at all. So me being in a semi sleep-deprived state assumed (there's that magical 7 letter word which causes trouble) that I must be able to have access to bounds because not only was I in the same namespace, I was in the same file.

After taking a little break, I finally realized my mistake, shook my head and decided to write a blog post about the ordeal. Morale of the story? Take a break when you're stuck on a problem you just can't figure out. And get some sleep.

The fixed solution:

namespace space
{

template<typename type> class ClassB;

template<typename type> class ClassA
{
protected:
 int bounds;
public:
 virtual ~ClassA(void)
 {
 }

 virtual int doIt(void) = 0;
 virtual int doIt2(void) = 0;
 
 friend class ClassB<type>;
};

template<typename type> class ClassB : public ClassA<type>
{
protected:
 ClassA<type> *parent;
public:
 virtual ~ClassB(void)
 {
 }

 virtual int doIt(void)
 {
  return bounds;
 }
 
 int doIt2(void)
 {
  return parent->bounds;
 }
};

}

Tuesday, February 26, 2013

Scene Navigation Zoom Tracking

In my last post I began working on a 2D scene manipulation system for an upcoming project. One of the design goals was to have a "smart zoom" feature which would track to where the mouse was. This is one of my favorite scene navigation tools as all I had to do to focus on a particular area was put my mouse in the center of that area and zoom in. I could also "pan" around the view by zooming out/in while moving my mouse around. Two applications that I know have this feature off the top of my head are SolidWorks and Eagle, though I really wish this feature was in all applications which require any type of scene navigation. I didn't have this feature implemented last time because I wasn't quite sure how to do it. However, I have since figured out how this feature can be implemented.

Tuesday, February 19, 2013

A Better 2D Scene Manipulation Widget for Qt

Introduction

I'm currently working on a project which requires a good 2D scene manipulation system. The application is being built with the Qt C++ framework. Initially I investigated using the base QGraphicsView class, but it really doesn't have the functionality I want so I'll have to roll my own solution somehow (either extend it or start from scratch).

For those curious as to what I want the ultimate look and feel to resemble, my inspiration for this design is based loosely off of other interfaces I've been highly impressed with. The two best similar type of interfaces I've used are the Blender and SolidWorks user interfaces. These programs are designed to work predominantly with 3D viewports, but my application only requires a 2D viewport. More details on the actual project will hopefully come later, depending on how committed I am to this project.

Tuesday, February 12, 2013

Intro to Microcontrollers

Introduction

Probably one of the greatest inventions in the world of electronics is the microcontroller. These are integrated circuit chips which can be programmed to interface with a wide variety of sensors, actuators, and other electrical components. They're general low-power and are fairly wimpy compared to an actual computer, or even many people's mobile devices.

I've had previous experience with microcontrollers using the Arduino Platform. This is a wonderful little board with an AVR microcontroller, USB port, and various IO pin functions. However, Arduinos aren't really that cheap. It's possible to get one for around $20 US, though they can get up to $60 or so if you get one of the bigger Arduino Megas. To contrast this, there are many microcontrollers available for under $5. These won't have all of the capabilities as the microcontroller used by Arduinos, but many times this isn't necessary. For my testing I'm going to use an ATtiny24A AVR microcontroller. It's an extremely low-power 14-pin microcontroller which can run up to 20 MHz. There's 2K of flash program memory, 128 bytes of EEPROM, and 128 bytes of SRAM.

Tuesday, February 5, 2013

Setting up Git on a Server

Introduction

I've been testing out Git a bit more recently. The main feature that interests me is the way Git branches. It's light-weight and is managed pretty well by the Git software. for more information, see Chapter 3 of the Git Book.

My plan is to use Git for synchronizing changes I make in my local development for my web server with the live version. I also want Git to manage a development server path which will switch to the branch I last pushed to for testing purposes. Luckily, both of these are fairly simple operations.

Tuesday, January 29, 2013

Keeping Track of App Settings in Java

Recently I was working on a Java application which required certain settings to be saved and reloaded, such as window positions, user data, etc. I thought it would be a great idea to use a centralized object which could keep track of the settings as many of them are shared between objects. As I got to thinking about how I could implement this centralized class, I imagined an XML file being saved to the drive which I would parse using a SAX parser, manually picking out the fields I wanted, and overall taking a ton of work. Everytime I wanted to add a new setting it involved going into my Settings class and adding a field and some custom SAX parsing code.

However, I realized that there was a much better way to do things, and that's to take advantage of the default Java Serialization API. This automatically handles the transfer of a Java object over a stream, which in my case are file streams so I can save and load the class. I also realized that rather than trying to push for efficiency and having the code completely valid by compile time, I would switch to a runtime management type approach. This involved Using a simple HashMap which would match objects with string key identifiers.

Tuesday, January 22, 2013

msysGit Context Menu Missing Issue Fix

This is a short fix for msysGit's context menus.

Something that's bothered me about the way msysGit context menus work is that they only work when you actually have a directory selected. You can't just click on the Windows Explorer "background" and launch the bash at that location.

The issue was brought up to the msysGit team but they didn't actually make any changes to fix the issue. Whether they haven't done it yet, don't know how, or intend this to be the correct behavior, I don't know. I just know it's not how I want it to be.

I could fix this by using the git-cheetah dll, but I don't really like this because it creates tons of menus I don't really want. It really doesn't fit into the way I use Git.

I use TortoiseGit for simple day-to-day use and the Git Bash when I need to do some heavy lifting. The less items I can have cluttering my already large explorer context menu the better.

Tuesday, January 15, 2013

Setting up a SVN Server

Introduction

I've recently added some revision control to my Ubuntu virtual server. I added Subversion and Git support. Here I'm documenting the process I used to setup a Subversion server.

I'm following for the most part Ubuntu's SVN tutorial.

Tuesday, January 8, 2013

SSH Key-Pair Authentication

Introduction

In the past I've limited SSH access to my Ubuntu virtual server by using static IP addresses. This works, but I've recently implemented a better method for ensuring SSH security for my virtual server. It uses the private/public key authentication method.

This is actually the method used pretty much everywhere else because it doesn't require hard-coded static IP addresses.

This is based mostly on the HowtoForge article about Key-Based SSH Logins with PuTTY.

Sunday, January 6, 2013

Commenting should now work

A quick service announcement, commenting should now work. You must be a registered user (either have a Google Account or some other OpenID account) in order to post a comment.

This is changed from previous settings where only members of the blog could post. I thought this meant that only people who joined the blog could post comments, and I would only have to remove/ban a user to keep spam down. Turns out this is not how this functions; it only lets people authorized to post in this blog to comment, too (currently only me).

As far as I can tell, there's no way to ban/block a particular user from posting. I'll see how these new settings work out, if it becomes a problem I may have to switch to a different scheme.

Tuesday, January 1, 2013

Automatic Preview

Introduction

This is a fun little post. I created a client-side preview box which automatically updates as the user types. It supports Alex Gorbatchev's Syntax Highlighter and MathJax.