Skip to content

Instantly share code, notes, and snippets.

View addam's full-sized avatar

Addam Dominec addam

View GitHub Profile
@addam
addam / barrier.js
Last active March 2, 2021 07:56
make sure that outdated web responses are ignored
class Barrier {
constructor() {
this.time = 0
this.state = 0
}
start() {
return ++this.time
}
@addam
addam / cesiumWorldPosition.glsl
Created September 8, 2020 13:25
get world coordinates in Cesium
vec4 world() {
vec4 ndc = vec4(
2.0 * (gl_FragCoord.xy - czm_viewport.xy) / czm_viewport.zw - 1.0,
(czm_readDepth(depthTexture, v_textureCoordinates) - czm_viewportTransformation[3][2]) / czm_viewportTransformation[2][2],
1.0);
vec4 eye = czm_inverseProjection * ndc;
return czm_inverseView * eye;
}
@addam
addam / printer.py
Last active December 11, 2019 08:48
simplify repeated calls to `print(..., file=f)` in Python
from functools import partial
class printer:
def __init__(self, *args):
self.args = args
def __enter__(self):
self.f = open(*self.args)
return partial(print, file=self.f)
def __exit__(self, *args):
self.f.__exit__(*args)
@addam
addam / board.py
Created September 14, 2019 09:32
Interactive board using a laser pointer
#!/usr/bin/python3
import cv2
import numpy
import pyautogui
# usage:
# after launch, a shot from the camera appears.
# mark four corners of the projector by clicking in order: top left, bottom left, bottom right, top right
# the program moves mouse when it sees the laser pointer on-screen
# the program never exits (you have to kill it)
@addam
addam / dlib_face_tracker.py
Created June 27, 2019 09:30
Quick face tracker using dlib + opencv. Face detector is slow, shape predictor is quick -- therefore they execute in separate threads.
#!/usr/bin/python3
import cv2 as cv
import dlib
from threading import Thread
import time
pose_model = dlib.shape_predictor("/usr/share/dlib/shape_predictor_68_face_landmarks.dat")
detector = dlib.get_frontal_face_detector()
def camera(source):
@addam
addam / x3d_viewer.html
Created December 4, 2018 14:42
A minimalist x3d viewer (useful for debugging postgis queries)
<html>
<head>
<title>X3D Viewer</title>
<script type='text/javascript' src='http://www.x3dom.org/download/x3dom.js'> </script>
<style>
canvas {
border: 1px solid gray;
}
</style>
</head>
@addam
addam / indent.js
Created November 4, 2018 19:29
Code indent function for <textarea onkeydown="tryIndent(event)">
function tryIndent(event) {
if (event.code != "Tab") {
return;
}
event.preventDefault();
var text = event.target;
var start = Math.max(text.value.lastIndexOf("\n", text.selectionStart - 1), 0);
var end = text.selectionEnd;
var str = text.value.slice(start, end);
var origLength = str.length;
@addam
addam / streambuf.cpp
Created May 31, 2018 20:00
Minimalist std::streambuf subclass for custom reading
#include <sstream>
#include <fstream>
#include <iostream>
#include <array>
class Buffer : public std::streambuf {
// we use ifstream here only for the sake of example
std::ifstream file;
std::array<char, 1024> data;
@addam
addam / postgres_copy.py
Last active November 7, 2017 20:13
Copy data from a posgre SQL server to another using asyncpg
#!/usr/bin/python3
import asyncio
import asyncpg
from time import time
try:
import uvloop
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
except:
pass # it will just be slower, no harm done
@addam
addam / move_if.hpp
Created September 20, 2017 05:28
C++ move_if: remove elements from a sequence and move them over elsewhere
template<typename It, typename OutIt, typename UnaryPredicate>
It move_if(It begin, It end, OutIt dst, UnaryPredicate pred)
{
It last = end;
while (begin != last) {
if (pred(*begin)) {
*dst++ = std::move(*begin);
std::swap(*begin, *(--last));
} else {
++begin;