Dimensions from photographs with perspective

I needed to calculate the dimensions of some historical buildings for which I only had photographs. A WAG is acceptable as I am only making rough models of the buildings. However, I did want to know how to do it with more accuracy and so looked around for an answer. SketchUp has a facility to do this and it is remarkably easy to use. Unfortunately, the rest of SketchUp is not as it is a technical tool that expects a trained user. So I kept looking around and finally found a clear explanation of the algorithm to use.

The key to understanding the algorithm is that you make right angled triangles in a plane perpendicular to a known height. For the rest of the explanation I am going to quote the author and present his diagram:

"Starting from the corner C of the building, draw a horizontal line on the image plane. This line represents a horizontal line in space, parallel to the image plane and at the same distance (and so the same length scale) as the vertical edge of the building above C, so you can measure lengths along it. It intersects the line AB, along the other side of the building, at B. (B is not necessarily the opposite corner of the building; this particular perspective view just happens to have the corners drawn about the same distance from the central corner A.) Now triangle ABC, in space, is a right triangle (assuming the corner A of the building is a right angle). You know the length BC of the hypotenuse (by measuring with a ruler calibrated to the vertical edge C, as I've shown with the green 45-45-90 triangle--just coincidentally, this is about the same height as the building for this drawing) and the length AC of one leg, so you can find the true length AB of the other leg by AB2=BC2-AC2.

"All this assumes that your camera isn't imposing too much distortion on the image. It would probably be a good idea to test this (take a picture of an object of known dimensions at similar lens zoom settings and with similar perspective) to see if this provides accuracy sufficient for your purposes."

Found at Dimensions from photographs with perspective.

3mm awe

Over at Lead Doesn't Bleed, Mr Blanchette painted 3mm Napoleonic figures using 14 applications of color! Remember that 3mm is the distance from foot to eye. And there are hundreds of them. I am in awe.

A bookmarklet for use with Kindle "Your Highlights" page

I heard Maria Popova's Kindle highlights lament on Tim Ferris's podcast and thought I would try to solve some part of it. Below is a bookmarklet that when used while viewing the highlights page will replace the page with data from the original page and then present in a format and style suitable for copy & pasting into Evernote. It has had VERY limited testing, but perhaps you will find it useful. Note that since this code uses HTML scraping to gather the data any change Amazon.com makes to the format of the original page may adversely affect the bookmarklet's functionality.

var O = $('<div class="books"/>');
var S;
var xs = $("#allHighlightedBooks").children();
for ( var i = 0; i < xs.length; i++ ) {
  var x = $(xs[i]);
  var c = x.attr("class");
  if ( c.indexOf("bookMain") != -1 ) {
    t = $('.title',x).text();
    a = $('.author',x).text();
    l = $('a',x).attr("href");
    b = $('<div class="book"/>').appendTo(O);
    $('<div class="title"/>').text(t).appendTo(b);
    $('<div class="author"/>').text(a).appendTo(b);
    $('<div class="link"><a href="'+l+'">'+l+'</a></div>').appendTo(b);
    S = $('<div class="selections"/>').appendTo(b);
  else if ( c.indexOf("highlightRow") != -1 ) {
    h = $('.highlight',x).text();
    n = $('.noteContent',x).text();
    l = $('.linkOut',x).attr("href");
    x = $('<div class="selection"/>').appendTo(S);
    $('<div class="selection"/>').text(h).appendTo(x);
    $('<div class="note"/>').text(n).appendTo(x);
    $('<div class="link"><a href="'+l+'">'+l+'</a></div>').appendTo(x);
  '<style>' +
  'div { margin-bottom: 1ex; }' + "\n" +
  '.title { font-weight: bold; }' +

The following code is also at https://gist.github.com/andrewgilmartin/26fd5b2ce02a3219c96c. Any suggestions on how to better use jQuery are most welcome.

Vote NO on 4

I am opposed to the $125M URI wants for a new engineering campus. It was only a few years ago that the university wanted to cease being a state school so they would not be under state imposed regulations and their concomitant costs. And yet now they want to be a state school so that they can get $125M from the taxpayers. When URI figures out what it wants its relationship with the people of RI to be then perhaps we can consider supporting this expenditure.

Digital layout comment

A comment on Video: Why eBook Text Layout is Terrible: The problem stems from using visual design features that were designed and refined for a fixed content, and a fixed size page and/or double page spread. There are examples today that are taking the steps needed to design a new visual + dynamic language for documents. When these become more broadly adopted I don't see us missing the widows and orphans.

Disable Chrome's print dialog box

Note to self ... For Mac Chrome users that don't want Chrome's print dialog box you can disable it from the command line using

defaults write com.google.Chrome DisablePrintPreview -boolean true

When the organization can consistently repeat the success ...

When I read headlines like "Building a Trading Platform in 6 Weeks in an Organization That Would Really Rather We Didn't" I think "So what? Anyone or any small team with sufficient domain knowledge, technical skills, and tools could can do that." A one time success is only that -- a one time success. When the organization, and not a specific person or specific team, can consistently repeat the success then tell me about it.

The Danes were victorious over the Normans this Wednesday

The Danes were victorious over the Normans this Wednesday. The Normans and Danes were slated for a final confrontation

The Dane's warlord (aka Cloudbeard, Kim, &, in spirit, Leo) directed a unit of hearthguard with all favorable dice on the battleboard to take down the Norman warlord. There were tense moments of rule reading by the Normans with intent on unhinging the Dane's psyche. When rules were read and positions measured the dice were gathered -- 12 dice for hearthguard and the 5 for the warlord -- and were rolled. The warlord was hit 5 times and now needed 4 saving throws to stay on the field of battle. Only one hit was countered. The Norman warlord, majestic on his white horse let go his shield, glowered at the unbelievers, and fell to ground dead.

An Observer pattern implementation and illiterate programming

This posting is an experiment in documenting Java code using (in my case) illiterate programming. The Java source code is at The Observer Pattern and the Perl code that converted the Java code is at Simpleminded Java + Javadoc to HTML Converter.

The Observer pattern is used to create a relationship between two objects. The relationship is usually unidirectional with the observer waiting for notices from the observed. The relationships is a loose one as the observed only needs to know of the interest of the observer and the observer only needs to know of the set of and ordering of notice the observed will send. A downside of this looseness is that all notices pass through a single observer method for further dispatching.

 10 package com.andrewgilmartin.common.observation;
 12 import java.util.Set;
 13 import java.util.concurrent.BlockingQueue;
 14 import java.util.concurrent.CopyOnWriteArraySet;
 15 import java.util.concurrent.LinkedBlockingQueue;

The Observation class is only used to group all the related interfaces and classes into one file. In a typical development environment each interface and class would be in its own file.

 22 public class Observation {

The Observable is the object that is being watched. As changes are prepared and then made the observable will send consent and information notices to the observers. The notices are normally specialized classes that hold some context about the change. For example, if the notice concerns the addition of new inventory to the warehouse then the notice's class could have a method for enumerating the new inventory.

 32     public interface Observable {

If it is necessary for the observable to have approval before making a change then a consent notification is sent to the observers. Each observer will be notified and if any observer opposes the change then it must return false. The current thread will be used to notify the observers and so all the observed must wait for all observers to consent.

 42         boolean consentNotification(Object notice);

An information notification is normally sent after a change. Since the change has already occurred the notices are typically sent asynchronously by a background thread. The order of the notices is preserved, however.

 50         void informationNotification(Object notice);
 51     }

The observer is the object that is notified by the observable. There is no typed relationship beyond the Observable and Observer classes. As mentioned earlier, the notices are usually of specialized classes where each notice instance holds data relevant to the change.

 59     public interface Observer {
 61         boolean notice(Observable observable, Object notice);
 62     }

A registry is the means of establishing the relationship between the observable and the observer. This interface is distinct from Observable as it is sometimes useful to indirectly register an observer via, for example, a registrar.

 70     public interface Registry {

The order of the observers is undefined.

 75         Set<Observer> getObservers();

Adds the observer to the set of observers. Returns true if the observer was added.

 81         boolean addObserver(Observer observer);

Removes the observer from the set of observers. Returns true if the observer was among the observers and was removed.

 87         boolean deleteObserver(Observer observer);
 88     }

There is often very little difference between implementations of Observable and Registry and so this base implementation can be widely employed by any class that wants to be observed.

When extending this class make sure to document the set of notices, their consent or information role, and what is their ordering.

 98     public static class ObservableBase implements Observable, Registry {

The management of the set of observers needs to be thread-safe. The set is expected to be mostly stable over the life of the observed and so copy-on-write semantics is appropriate here.

105         private final Set<Observer> observers = new CopyOnWriteArraySet<Observer>();

Information notification notices will be sent by a background thread. A blocking queue will be used to coordinate the passing of notices from the observed to this background thread.

111         private final BlockingQueue<Object> informationNotices = new LinkedBlockingQueue<Object>();
113         public ObservableBase() {

This implementation of the information notification background thread is quite simple and so uses an anonymous class for the implementation.

119             Thread informationEventsDispatcher = new Thread(new Runnable() {
120                 @Override
121                 public void run() {
122                     try {

Here the thread waits for a new notice on the queue and then sends it to each of the current observers.

127                         for (;;) {
128                             Object notice = informationNotices.take();
129                             for (Observer observer : observers) {
130                                 observer.notice(ObservableBase.this, notice);
131                             }
132                         }
133                     }
134                     catch (InterruptedException e) {
135                         // empty
136                     }
137                 }
138             });
139             informationEventsDispatcher.setDaemon(true);
140             informationEventsDispatcher.start();
141         }
143         @Override
144         public boolean consentNotification(Object notice) {

As mentioned earlier, consent notifications are performed by the observed's thread. In this way, as soon as any observer opposes the change the observed must reject the change.

150             for (Observer observer : observers) {
151                 if (!observer.notice(this, notice)) {
152                     return false;
153                 }
154             }
155             return true;
156         }
158         @Override
159         public void informationNotification(Object notice) {

Pass along the notice to the background thread.

163             informationNotices.add(notice);
164         }
166         @Override
167         public Set<Observer> getObservers() {
168             return observers;
169         }
171         @Override
172         public boolean addObserver(Observer observer) {
173             return observers.add(observer);
174         }
176         @Override
177         public boolean deleteObserver(Observer observer) {
178             return observers.remove(observer);
179         }
180     }

Here is a small example of using the observation interfaces and classes.

185     public static void main(String... args) throws Exception {
187         class Notice {
189             private int senderId;
190             private int sequenceNumber;
192             public Notice(int senderId, int sequenceNumber) {
193                 this.senderId = senderId;
194                 this.sequenceNumber = sequenceNumber;
195             }
197             public int getSenderId() {
198                 return senderId;
199             }
201             public int getSequenceNumber() {
202                 return sequenceNumber;
203             }
204         }

The Sender is an observed. All that it does it to send a stream of notices consisting of sender-id & sequence-number pairs.

210         class Sender extends ObservableBase implements Runnable {
212             private int senderId;
214             public Sender(int id) {
215                 this.senderId = id;
216             }
218             @Override
219             public void run() {
220                 for (int sequenceNumber = 0;; sequenceNumber++) {
221                     informationNotification(new Notice(senderId, sequenceNumber));
222                     sleep(); // add some randomness to the processing.
223                 }
224             }
225         }

The Receiver is an observer. All that it does is to print the notice's facts. In this example all notices are information notifications and so the notice() return value does not matter. However, as a matter of course, notice() should always return true unless it is well sure of the consequences of opposing the change.

234         class Receiver implements Observer {
236             private int receiverId;
238             public Receiver(int id) {
239                 this.receiverId = id;
240             }
242             @Override
243             public boolean notice(Observable observable, Object notice) {
244                 if (notice instanceof Notice) {
245                     Notice n = (Notice) notice;
246                     System.out.printf("notice %d %d %d\n", receiverId, n.getSenderId(), n.getSequenceNumber());
247                     sleep(); // add some randomness to the processing.
248                 }
249                 return true;
250             }
251         }

Create a few senders.

256         Sender[] senders = new Sender[5];
257         for (int i = 0; i < senders.length; i++) {
258             senders[i] = new Sender(i);
259         }

Create a few receivers.

264         Receiver[] receivers = new Receiver[3];
265         for (int i = 0; i < receivers.length; i++) {
266             receivers[i] = new Receiver(i);
267         }

Have each receiver observe each sender

272         for (Receiver r : receivers) {
273             for (Sender s : senders) {
274                 s.addObserver(r);
275             }
276         }

Startup the senders

281         for (Sender s : senders) {
282             Thread t = new Thread(s);
283             t.setDaemon(false);
284             t.start();
285         }
286     }

Add some sleep of random duration to the current thread.

291     static void sleep() {
292         try {
293             Thread.sleep(Math.round(Math.random() * 25));
294         }
295         catch (InterruptedException e) {
296             // empty
297         }
298     }
299 }

To build and run this from the command line, first compile using

303 javac -d /tmp ./src/com/andrewgilmartin/common/observation/Observation

And then run using

308 java -classpath /tmp com.andrewgilmartin.common.observation.Observation

You can show to yourself that the events are ordered by sorting the output on the receiver id and you will see that the events are in numeric order.

314 java -classpath /tmp com.andrewgilmartin.common.observation.Observation | head -20 | sort -k 2 -n -s

318 // END

Her Majesty's Rugged Constabulary in preparation

MySql load balancing with HAProxy

We use pair of HAProxy servers at work to route and balance all of our traffic. It is a highly reliable tool with little time or performance overhead. Yesterday I need to balance a load of MySql connections across several slave servers and so used HAProxy to do that. Since I did not find the exact recipe I need on the web here is my test configuration contribution to the MySql global distributed cookbook:

    mode http
    stats uri /haproxy-stats
    stats enable
    log global
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms

listen admin
    bind *:8000
    mode http

listen mysql
    bind *:3306
    balance  leastconn
    mode     tcp
    option   tcpka
    option   srvtcpka
    option   mysql-check user haproxy
    server mysql1 weight 1 maxconn 200 check
    server mysql2 weight 1 maxconn 200 check
    server mysql3 weight 1 maxconn 200 check


Each MySql server used must have the haproxy user created using

create user 'haproxy'@'%';
flush privileges;

The wildcard can be replaced with the IP address of the HAProxy server.

I want to thank the folks at Homebrew for the haproxy formula.

Low tech needed

And how is this better than writing the inception and expiration dates on the container with a grease pencil?

Given the pace of ...

Where is the call from?

I wish my phone told me where the person was calling from and not where there phone number was registered. I would have been much more prepared to take a call from Matunuck, RI than from Long Beach, CA.